Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
coryatcf
Visitor

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.
0 Kudos
12 REPLIES 12
RokuJoel
Binge Watcher

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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
0 Kudos
destruk
Streaming Star

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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?)
0 Kudos
RokuJoel
Binge Watcher

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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
0 Kudos
coryatcf
Visitor

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

Thanks for the responses. How would I code such a delay?
0 Kudos
RokuJoel
Binge Watcher

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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
0 Kudos
coryatcf
Visitor

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

Great, thanks! 🙂
0 Kudos
coryatcf
Visitor

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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?
0 Kudos
coryatcf
Visitor

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

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?
0 Kudos
RokuMarkn
Visitor

Re: Making Simplevideoplayer Try to Access Stream Repeatedly

What do you see on the debug console?

--Mark
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.