Skip to content

Instantly share code, notes, and snippets.

@matula
Created May 5, 2017 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matula/eec01b7265c9b1be05e8f44768970ab1 to your computer and use it in GitHub Desktop.
Save matula/eec01b7265c9b1be05e8f44768970ab1 to your computer and use it in GitHub Desktop.
Library "Roku_Ads.brs"
sub init()
print "PlayerTask.brs [init]"
m.top.functionName = "playContentWithAds"
m.top.id = "PlayerTask"
end sub
sub playContentWithAds()
print "PlayerTask.brs [playContentWithAds]"
video = m.top.video
' `view` is the node under which RAF should display its UI (passed as 3rd argument of showAds())
view = video.getParent()
RAF = Roku_Ads()
content = video.content
RAF.setAdUrl(content.ad_url)
RAF.setAdPrefs(true, 1) ' Roku ads as fallback
keepPlaying = true 'gets set to `false` when showAds() was exited via Back button
port = CreateObject("roMessagePort")
if keepPlaying then
print "PlayerTask.brs [playContentWithAds - keep playing]"
video.observeField("position", port)
video.observeField("state", port)
video.visible = true
video.control = "play"
video.setFocus(true) 'so we can handle a Back key interruption
end if
curPos = 0
currentIndex = 0
adPods = invalid
isPlayingPostroll = false
while keepPlaying
msg = wait(0, port)
if type(msg) = "roSGNodeEvent"
if msg.GetField() = "position" then
' keep track of where we reached in content
curPos = msg.GetData()
' Drop an ad between playlist videos
if currentIndex <> video.contentIndex
adPods = RAF.getAds()
if adPods <> invalid and adPods.count() > 0
print "PlayerTask: ads, stopping video"
SaveVideoView(true, content.video_ids[video.contentIndex])
'ask the video to stop - the rest is handled in the state=stopped event below
video.control = "stop"
else
SaveVideoView(false, content.video_ids[video.contentIndex])
currentIndex = video.contentIndex
end if
end if
else if msg.GetField() = "state" then
curState = msg.GetData()
print "PlayerTask: state = "; curState
if curState = "stopped" then
currentIndex = video.contentIndex
if adPods = invalid or adPods.count() = 0 then
exit while
end if
print "PlayerTask: playing midroll/postroll ads"
keepPlaying = RAF.showAds(adPods, invalid, view)
adPods = invalid
if isPlayingPostroll then
exit while
end if
if keepPlaying then
print "PlayerTask: mid-roll finished, seek to "; stri(curPos)
video.visible = true
video.seek = curPos
video.control = "play"
video.setFocus(true) 'important: take the focus back (RAF took it above)
end if
else if curState = "finished" then
print "PlayerTask: main content finished"
' render post-roll ads
adPods = RAF.getAds(msg)
if adPods = invalid or adPods.count() = 0 then
exit while
end if
print "PlayerTask: has postroll ads"
isPlayingPostroll = true
' stop the video, the post-roll would show when the state changes to "stopped" (above)
video.control = "stop"
end if
end if
end if
end while
print "PlayerTask: exiting playContentWithAds()"
end sub
sub SaveVideoView(adFilled, videoId)
' Save Video View
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment