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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Channel Surfer

pre roll ad in mini video player using RAF

I am trying to play pre roll ad using RAF in small video player i.e video player window is 25% smaller (height and width) of TV screen but the issue I am facing soon as player starts to playing ad, it alwas plays ad in full screen and it is not respecting height width which supplied by me to player (video node). I tried changing height width of parent view of video node but it did not helped me. Can you please help me on this and can you suggest me how should I acheive this? 

0 Kudos
Binge Watcher

Re: pre roll ad in mini video player using RAF

Hello @JalpaWhiz I've spent a lot of time figuring out a way to handle the mini player with RAF. For the project I am working on, it's not viable to use the SGDEX framework due to the significant rework required for integration. SGDEX is an option, but I'll provide a solution using the regular RAF. I believe you are using the showAds() function from the RAF library. You need to create an overlay layer for your view and pass it for showAds(), the view usually is video.getParent(). By doing this, you ensure that the new overlay is on top of the video view. You can then resize it using the overlay scale. In this example, I am using 0.58 of the full 1080 size, but you can adapt it to your size. The translation 0,0 means it will be positioned at the same x and y coordinates as the parent. You can also choose different x and y coordinates if you want to move it, but I believe you want this on the same video player at a reduced size. Here is the example of what I said : 


m.overlayNode = createObject("roSGNode", "Group")
view = video.getParent()
if isFullScreen = false
    ' Create a new overlay node to display ads
    m.overlayNode.translation = [0, 0] ' Adjust as needed to position over the video
    m.overlayNode.scale = [0.58, 0.58] ' Adjust the scale as needed
    ' Add the overlay node to the top of the screen (or relevant parent)
    screen = view
end if

'Your Call for preroll ads
video.control = "stop"
if isFullScreen = true
    keepPlaying = RAF.showAds(adPods, invalid, view)
    keepPlaying = RAF.showAds(adPods, invalid, m.overlayNode)
end if


I hope this helps you. You will probably face issues with RAF grabbing focus for the overlay but a workaround would be to create a custom ads callback and use RAF.SetTrackingCallback function to identify when the ad started for you to set Focus for your video root parent. It's tricky since RAF needs the focus to itself to handle a lot of internal important stuff.

Channel Surfer

Re: pre roll ad in mini video player using RAF

Thanks a lot bro you done a million dollar job.


Re: pre roll ad in mini video player using RAF

How do you handle focus and give it back to custom player controls. observing focusedChild from "view" node and it becomes invalid. No problem if i give invalid for view.

0 Kudos
Binge Watcher

Re: pre roll ad in mini video player using RAF

Hi @Mavzu I've observed the ads state callback that you know if it's starting and ad. If it begins to you can give the focus back to your player but notice if you are navigating around keep in mind RAF does not permit that since it will grab/force the focus to the RAF container.
Verify if you can find github samples about SetTrackingCallback which I've used here: RAF.SetTrackingCallback(customCallback) because I didn't see this as a part of official documentation but it's have being used by the community and worked fine for me.


sub customCallback(obj = invalid as Dynamic, evtType = invalid as Dynamic, ctx = invalid as Dynamic)

if ctx.type = "Start"
print "Set the focus back to the video node!"
video = getVideoNode()
end if
' You can monitor others states here and also identify ad errors
end sub




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.