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: 
Blackhawk
Roku Guru

Problem with the PlayerTask

Im making an ad supported channel.

' ********** Copyright 2016 Roku Corp.  All Rights Reserved. ********** 
' inits details screen
' sets all observers
' configures buttons for Details screen
Function Init()
? "[DetailsScreen] init"

m.top.observeField("visible", "onVisibleChange")
m.top.observeField("focusedChild", "OnFocusedChildChange")

m.buttons = m.top.findNode("Buttons")
m.poster = m.top.findNode("Poster")
m.description = m.top.findNode("Description")
m.background = m.top.findNode("Background")


' create buttons
result = []
for each button in ["Play", "Second button"]
result.push({title : button})
end for
m.buttons.content = ContentList2SimpleNode(result)
End Function

' set proper focus to buttons if Details opened and stops Video if Details closed
Sub onVisibleChange()
? "[DetailsScreen] onVisibleChange"
if m.top.visible = true then
m.buttons.jumpToItem = 0
m.buttons.setFocus(true)
else
m.videoPlayer.visible = false
m.videoPlayer.control = "stop"
m.poster.uri=""
m.background.uri=""
end if
End Sub

' set proper focus to Buttons in case if return from Video PLayer
Sub OnFocusedChildChange()
if m.top.isInFocusChain() and not m.buttons.hasFocus() and not m.videoPlayer.hasFocus() then
m.buttons.setFocus(true)
end if
End Sub

' set proper focus on buttons and stops video if return from Playback to details
Sub onVideoVisibleChange()
if m.Player.visible = false and m.top.visible = true
m.buttons.setFocus(true)
m.Player.control = "stop"
end if
End Sub

sub PlayerStateChanged()
print "EntryScene: PlayerStateChanged(), state = "; m.Player.state
if m.Player.state = "done" or m.Player.state = "stop"
m.Player.visible = false
m.list.setFocus(true) 'NB. the player took the focus away, so get it back
end if
end sub

' on Button press handler
Sub onItemSelected()
' first button is Play
' compile into a VideoContent node
content = CreateObject("roSGNode", "VideoContent")

if m.Player = invalid:
m.Player = m.top.CreateChild("Player")
m.Player.observeField("state", "PlayerStateChanged")
end if

'start the player
m.Player.content = content
m.Player.visible = true
m.Player.control = "play"
End Sub

' Content change handler
Sub OnContentChange()
m.description.content = m.top.content
m.description.Description.width = "770"
m.Player.content = m.top.content
m.top.streamUrl = m.top.content.url
m.poster.uri = m.top.content.hdBackgroundImageUrl
m.background.uri = m.top.content.hdBackgroundImageUrl
End Sub

'///////////////////////////////////////////'
' Helper function convert AA to Node
Function ContentList2SimpleNode(contentList as Object, nodeType = "ContentNode" as String) as Object
result = createObject("roSGNode",nodeType)
if result <> invalid
for each itemAA in contentList
item = createObject("roSGNode", nodeType)
item.setFields(itemAA)
result.appendChild(item)
end for
end if
return result
End Function


And i keep getting this error



------ Compiling dev 'Simple Grid with Details and Video' ------
*** ERROR compiling /pkg:/source/PlayerTask.brs:
SUB or FUNCTION defined twice. (compile error &had) in pkg:/source/PlayerTask.brs(11) 'init'

And i checked this code


'*********************************************************************
'** (c) 2016-2017 Roku, Inc. All content herein is protected by U.S.
'** copyright and other applicable intellectual property laws and may
'** not be copied without the express permission of Roku, Inc., which
'** reserves all rights. Reuse of any of this content for any purpose
'** without the permission of Roku, Inc. is strictly prohibited.
'*********************************************************************

Library "Roku_Ads.brs"

sub init()
m.top.functionName = "playContentWithAds"
m.top.id = "PlayerTask"
end sub

sub 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()
'RAF.clearAdBufferScreenLayers() ' in case it was set earlier
'RAF.enableAdBufferMessaging(true, true) ' could have been cleared by custom screen
'RAF.setAdBufferScreenContent({})

content = video.content
RAF.setAdUrl(content.ad_url)
' for generic measurements api
RAF.setContentGenre(content.categories) 'if unset, ContentNode has it as []
' Nielsen DAR specific measurements
if content.nielsen_app_id <> invalid:
RAF.enableNielsenDAR(true)
RAF.setNielsenAppId(content.nielsen_app_id)
RAF.setNielsenGenre(content.nielsen_genre)
RAF.setNielsenProgramId(content.nielsen_program_id)
RAF.setContentLength(content.length)
end if

' log tracking events
' logObj = {
' log : Function(evtType = invalid as Dynamic, ctx = invalid as Dynamic)
' if GetInterface(evtType, "ifString") <> invalid
' print "*** tracking event " + evtType + " fired."
' if ctx.companion = true then
' print "***** companion = true"
' end if
' if ctx.errMsg <> invalid then print "***** Error message: " + ctx.errMsg
' if ctx.adIndex <> invalid then print "***** Ad Index: " + ctx.adIndex.ToStr()
' if ctx.ad <> invalid and ctx.ad.adTitle <> invalid then print "***** Ad Title: " + ctx.ad.adTitle
' else if ctx <> invalid and ctx.time <> invalid
' print "*** checking tracking events for ad progress: " + ctx.time.ToStr()
' end if
' End Function
' }
' logFunc = Function(obj = Invalid as Dynamic, evtType = invalid as Dynamic, ctx = invalid as Dynamic)
' obj.log(evtType, ctx)
' End Function
' RAF.setTrackingCallback(logFunc, logObj)

adPods = RAF.getAds() 'array of ad pods
keepPlaying = true 'gets set to `false` when showAds() was exited via Back button

' show the pre-roll ads, if any
if adPods <> invalid and adPods.count() > 0
keepPlaying = RAF.showAds(adPods, invalid, view)
end if

port = CreateObject("roMessagePort")
if keepPlaying then
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
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()
' check for mid-roll ads
adPods = RAF.getAds(msg)
if adPods <> invalid and adPods.count() > 0
print "PlayerTask: mid-roll ads, stopping video"
'ask the video to stop - the rest is handled in the state=stopped event below
video.control = "stop"
end if
else if msg.GetField() = "state" then
curState = msg.GetData()
print "PlayerTask: state = "; curState
if curState = "stopped" then
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


Yet i can't find an error
0 Kudos
5 REPLIES 5
renojim
Community Streaming Expert

Re: Problem with the PlayerTask

Do you have 2 files in your source directory with an init function?
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Blackhawk
Roku Guru

Re: Problem with the PlayerTask

Just Player and PlayerTask
0 Kudos
renojim
Community Streaming Expert

Re: Problem with the PlayerTask

You can't have 2 files in your source directory with the same function (init in your case). Perhaps you meant to have them in the components directory.

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Blackhawk
Roku Guru

Re: Problem with the PlayerTask

I moved it to components and now I have a new error

=================================================================
Found 1 compile error in file pkg:/components/Player.brs included from file pkg:/components/screens/Player.xml
--- Error loading file. (compile error &hb9) in pkg:/components/Player.brs
=================================================================
Found 1 compile error in file pkg:/components/PlayerTask.brs included from file pkg:/components/screens/PlayerTask.xml
--- Error loading file. (compile error &hb9) in pkg:/components/PlayerTask.brs

=================================================================
An error occurred while attempting to compile the application's components:
-------> Error parsing multiple XML components (Player, PlayerTask)
0 Kudos
renojim
Community Streaming Expert

Re: Problem with the PlayerTask

The .brs and .xml files with the same names should probably be in the same directory.  Look in the XML files for something like:
<script type="text/brightscript" uri="pkg:/components/screens/Player.brs" />

In this case, they'd both go in the components/screens directory.

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
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.