Making Simplevideoplayer Try to Access Stream Repeatedly

I would like to adapt the simplevideoplayer script from the SDK so that once a streaming video source is started to play, and then the source stops streaming, the script will continue attempting to reconnect to the stream indefinitely, and not kick back to the Roku home screen. Is this possible?

I am a total newbie to Roku development, and would appreciate any suggestions.

Instead of calling displayvideo() if msg.getindex()=1, you would create another function like this:


function loopvideo() as integer
     continue=1
     while continue=1
          continue=displayvideo()
     end while
end function

and then modify the displayvideo function so that unless the user cancels playback, it always try’s to play the content, in this case, by returning the value of 1. First modify the function so it returns an integer value:

function displayvideo(args As Dynamic) as integer

Then make sure that a 1 is returned in all circumstances except when the user closes the screen:


   while true
      msg = wait(0, video.GetMessagePort())
      if type(msg) = "roVideoScreenEvent"
        if msg.isScreenClosed() then 'ScreenClosed event
                print "user cancelled playback, closing video screen"
                 return 0 
            else if msg.isPlaybackPosition() then
                nowpos = msg.GetIndex()
                if nowpos > 0
                    if abs(nowpos - lastSavedPos) > statusInterval
                        lastSavedPos = nowpos
                    end if
                end if
             else if msg.isRequestFailed()
                 return 1
             else
                return 1
             end if
        end if
   end while
   return 1

Hope that helps.

  • Joel

Adding some kind of user feedback might be a good idea so the user knows it’s doing something if it’s ‘retrying stream’ or ‘re-contacting server’ to play content.
And I’m not sure if the server host would like being DOS’d with thousands of requests from multiple roku boxes when the stream goes down so it might be a good idea to include a delay between retries (a minute maybe?)

Good points. Probably even a few seconds delay, 5-15 would be enough to reduce the severity if the server went down, or the stream was not available.

  • Joel

Thanks for the responses. How would I code such a delay?

function loopvideo() as integer
     continue=1
     while continue=1
          continue=displayvideo()
          if continue=1 then 
               sleep(5000) 'five second delay
          end if
     end while
end function

Great, thanks! :slightly_smiling_face:

Joel,

Okay, I’m actually looking at the simplevideoplayer code now, and am guessing that your first code example was showing a concept, not actual code to use for the modification I had in mind?

Here is what I have:


' ********************************************************************
' **  Simplevideoplayer - adapted by Cory 6-29-2012
' ********************************************************************

Sub Main(args As Dynamic)
    displayVideo(" ")  
End Sub

'*************************************************************
'** displayVideo()
'*************************************************************

Function displayVideo(args As Dynamic) as integer
    print "Displaying video: "
    p = CreateObject("roMessagePort")
    video = CreateObject("roVideoScreen")
    video.setMessagePort(p)

    'bitrates  = [0]          ' 0 = no dots, adaptive bitrate
    'bitrates  = [348]    ' <500 Kbps = 1 dot
    'bitrates  = [664]    ' <800 Kbps = 2 dots
    'bitrates  = [996]    ' <1.1Mbps  = 3 dots
    'bitrates  = [2048]    ' >=1.1Mbps = 4 dots
    bitrates  = [0]    

    ' Live stream from Wowza server
    urls = ["http://myserver:1935/live/livestream.sdp/playlist.m3u8"]
    qualities = ["SD"]
    streamformat = "hls"
    title = "Livestream"
    srt=""

    if type(args) = "roAssociativeArray"
        if type(args.url) = "roString" and args.url <> "" then
            urls[0] = args.url
        end if
        if type(args.StreamFormat) = "roString" and args.StreamFormat <> "" then
            StreamFormat = args.StreamFormat
        end if
        if type(args.title) = "roString" and args.title <> "" then
            title = args.title
        else 
            title = ""
        end if
        if type(args.srt) = "roString" and args.srt <> "" then
            srt = args.StreamFormat
        else 
            srt = ""
        end if
    end if
    
    videoclip = CreateObject("roAssociativeArray")
    videoclip.StreamBitrates = bitrates
    videoclip.StreamUrls = urls
    videoclip.StreamQualities = qualities
    videoclip.StreamFormat = StreamFormat
    videoclip.Title = title
    print "srt = ";srt
    if srt <> invalid and srt <> "" then
        videoclip.SubtitleUrl = srt
    end if
    
    video.SetContent(videoclip)
    video.show()

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

   while true
      msg = wait(0, video.GetMessagePort())
      if type(msg) = "roVideoScreenEvent"
        if msg.isScreenClosed() then 'ScreenClosed event
                print "user cancelled playback, closing video screen"
                 return 0 
            else if msg.isPlaybackPosition() then
                nowpos = msg.GetIndex()
                if nowpos > 0
                    if abs(nowpos - lastSavedPos) > statusInterval
                        lastSavedPos = nowpos
                    end if
                end if
             else if msg.isRequestFailed()
                 return 1
             else
                return 1
             end if
        end if
   end while
   return 1
End Function

This should take the user right from my channel icon on the home screen to my streaming video, and stay there attempting to play the video stream until the user closes the screen (i.e. presses the “home” button, etc.), right?

Where would I insert the delay?

Okay, I tried this on a Roku now, and no joy… It appears to start to load the video clip, but then stops and kicks back to the home screen. Can anyone see what I am doing wrong here?

What do you see on the debug console?

–Mark

I’m not sure how to access the debug console (telnet to port 8085?). The application installs successfully, so I don’t think there is a problem with the code syntax, I just don’t know how to write it to do what I want it to do.

Yes, telnet to 8085. You’re really handicapping yourself if you don’t use the debug console.

–Mark

Ok, here’s what I’m getting in debug:

------ Running ------
Displaying video:
srt =

Just the same as when I’m watching video with an app that does work.