Playing simple video in Roku sdk

Posted By : Deepak Kumar | 29-May-2014

This blog will help you to understand , how a video is played on roku sdk.

The application starts from main , it simply configures theme attributes like image,logo and a short desciption about the video.

Sub Main()
    initTheme()  'sets attributes for themes like image logo,title.

Now initialize values to be set further for your sceen board .values like title ,desciption,length , content type for video.

 itemAttributes = {
	       ContentType:"episode"
               SDPosterUrl:"file://pkg:/images/CraigVenter.jpg"
               HDPosterUrl:"file://pkg:/images/CraigVenter.jpg"                         
               Description:"In this video Craig Venter talks about his research on fourth-generation fuels. He discusses about generating new chromosomes using digital technology, the causes why one should do this and bioethics of synthtic life."           
               Length:1972               
               Title:"Craig Venter asks, Can new life be created out of this digital universe ?"
               }
showItemDetails(itemAttributes)    
End Sub
Function showItemDetails(itemAttributes as object) As Boolean
    port = CreateObject("roMessagePort")
    boardScreen = CreateObject("roSpringboardScreen")    
    boardScreen.SetMessagePort(port)

To defer screen updates temporarily to avoid flashing.

    boardScreen.AllowUpdates(false)  
    if itemAttributes <> invalid and type(itemAttributes) = "roAssociativeArray"
        boardScreen.SetContent(itemAttributes)
    endif
    boardScreen.SetDescriptionStyle("generic") 'You may also use audio, movie, video, generic, generic+episode=4x3,

This will clear buttons ,if any appears on screen board

    boardScreen.ClearButtons()	   

This will add a button on your screen board.

    boardScreen.AddButton(1,"Play") 
    boardScreen.AddButton(2,"Go Back")

If you want to disable ratings on screen, set this to false

    boardScreen.SetStaticRatingEnabled(false) 
    boardScreen.AllowUpdates(true)
    boardScreen.Show()
    downKey=3
    selectKey=6

Now we want that video must play if someone press button "Play" ,process waits untill any event occurs . If "Play" button is selected than this calls playVideo function to play video.

    while true
        message = wait(0, boardScreen.GetMessagePort())
        if type(message) = "roSpringboardScreenEvent"
            if message.isScreenClosed()
                print "Screen exit"
                exit while                
            else if message.isButtonPressed()
                    print "Button pressed: "; message.GetIndex(); " " message.GetData()
                    if message.GetIndex() = 1
                         playVideo()
                    else if message.GetIndex() = 2
                         return true
                    endif
            else
                print "Unknown event: "; message.GetType(); " message: "; message.GetMessage()
            endif
        else 
            print "Incorrect type.... type=";message.GetType(); " message: "; message.GetMessage()
        endif
    end while


    return true
End Function

To play video we will have to configure "roVideoScreen" object set values like url, qualities,bitrates etc. Here how i did this.

Function playVideo()
    print "Displaying video: "
    port = CreateObject("roMessagePort")
    videoScreen = CreateObject("roVideoScreen")
    videoScreen.setMessagePort(port)    
    
    videoclip = CreateObject("roAssociativeArray")

Set the bitrates of video

    videoclip.StreamBitrates = [0]

Set the url of video


    videoclip.StreamUrls = ["http://video.ted.com/talks/podcast/CraigVenter_2008_480.mp4"]

Quality of video

    
    videoclip.StreamQualities = ["HD"]

Set the video of format

    videoclip.StreamFormat = "mp4"
    videoclip.Length= 1972
    videoclip.Title = "Craig Venter Synthetic Life"
    
    videoScreen.SetContent(videoclip)
    videoScreen.show()

    lastSavedPos   = 0
    statusInterval = 10 'position must change by more than this number of seconds before saving

To forward movie by 10 seconds, the following code checks for the current playposition and increases it by 10.

    while true
        message = wait(0, videoScreen.GetMessagePort())
        if type(message) = "roVideoScreenEvent"
            if message.isScreenClosed() then 'ScreenClosed event
                print "Closing video screen"
                exit while
            else if message.isPlaybackPosition() then
                nowpos = message.GetIndex()
                if nowpos > 10000
                    
                end if
                if nowpos > 0
                    if abs(nowpos - lastSavedPos) > statusInterval
                        lastSavedPos = nowpos
                    end if
                end if
            else if message.isRequestFailed()
                print "play failed: "; message.GetMessage()
            else
                print "Unknown event: "; message.GetType(); " message: "; message.GetMessage()
            endif
        end if
    end while
End Function
Board Screen will look like this.
clickdownload, to download this example.
Thanks!!!!

About Author

Author Image
Deepak Kumar

Deepak is a bright Web App Developer, and has good knowledge of Core Java, Advance Java (Servlet, JSP, JSTL) and Hibernate, JUnit.

Request for Proposal

Name is required

Comment is required

Sending message..