Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
Showing results for 
Search instead for 
Did you mean: 
Level 7

Preroll Ads

I need assistance customizing the videoplayer example with the VAST demo to add preroll functionality. Here is what I've done so far:

1. Added NWM_VAST.brs, VAST_VideoScreen.brs, and NWM_Utilities.brs to my project.
2. I placed the following code in appMain:

vast = NWM_VAST()
vastURL = ""
util = NWM_Utilities()
raw = util.GetStringFromURL(vastURL)
if > 0 and[0].video <> invalid
video.preroll =[0].video
end if

3. I replaced appVideoScreen.brs with VAST_VideoScreen. This is the code I am using:
sub PlayVideo(video)
canvas = CreateObject("roImageCanvas")
canvas.SetLayer(1, {color: "#000000"})

' play the pre-roll
adCompleted = true

if video.preroll <> invalid
adCompleted = ShowPreRoll(canvas, video.preroll)
end if

if adCompleted
' if the ad completed without the user pressing UP, play the content
end if

end sub

sub ShowVideoScreen(episode)
screen = CreateObject("roVideoScreen")

while true
msg = wait(0, screen.GetMessagePort())

if msg <> invalid
if msg.isScreenClosed()
exit while
end if
end if
end while

end sub

function ShowPreRoll(canvas, ad)
result = true

player = CreateObject("roVideoPlayer")
' be sure to use the same message port for both the canvas and the player

' set up some messaging to display while the pre-roll buffers
canvas.SetLayer(2, {text: "Your program will begin after this message"})


while true
msg = wait(0, canvas.GetMessagePort())

if type(msg) = "roVideoPlayerEvent"
if msg.isFullResult()
exit while
else if msg.isPartialResult()
exit while
else if msg.isRequestFailed()
print "isRequestFailed"
exit while
else if msg.isStatusMessage()
if msg.GetMessage() = "start of play"
' once the video starts, clear out the canvas so it doesn't cover the video
canvas.SetLayer(1, {color: "#00000000", CompositionMode: "Source"})
end if


I have spent hours over the past severals days attempting to implement preroll ads. I've read the blog post detailing preroll ads MANY times, and I've done a lot of reading and research too. Practical help would be very appreciated!
0 Kudos
4 Replies
Roku Employee
Roku Employee

Re: Preroll Ads

Just replacing one video screen with the other is unlikely to work. And I don't see where you're calling PlayVideo() in the first chunk of code, after building the preroll.

Can you describe the problems your having in more detail? Maybe share a crash log or the console output associated with the bad behavior?
0 Kudos
Level 7

Re: Preroll Ads

Can someone from Roku please contact me regarding these VAST files? I am looking to run ads using VAST and these would be an incredible help. Thanks!!!
0 Kudos
Roku Employee
Roku Employee

Re: Preroll Ads

Hi, here is a link to RokuChris' VAST example:

- Joel
0 Kudos
Level 7

Re: Preroll Ads

Just want clarification on the right to use this library.

"To this purpose, this library is designed to facilitate advanced targeting and inventory optimization for any ads served by Roku. Even if all ads are served by a third party ad provider, it is advantageous to fully implement this library should Roku have an advertising agreement with you in the future."

Can anybody use this library for running VAST video ads? Can it be included in the 3rd party library this is provided to developer?
0 Kudos