我正试图将VAST视频预卷广告添加到SThree示例Roku SDK项目("带有视频和详细信息页面的简单网格(.zip)"版本)。
我可以通过将以下代码放入main.brs
来让它在应用程序启动上工作:
library "Roku_Ads.brs"
...
Sub RunUserInterface()
...
raf = Roku_Ads()
raf.setAdPrefs(false) 'disable back-filled ads
raf.setDebugOutput(true)
raf.setAdUrl("http://pubads.g.doubleclick.net/gampad/ads?slotname=/82114269/Video/fallbacktest&sz=1920x1080&url=[http://roku.com&unviewed_position_start=1&output=vast&impl=s&env=vp&gdfp_req=1&ad_rule=0&description_url=http%3A%2F%2Fapps.roku.com&vad_type=linear&sdkv=roku&min_ad_duration=0&max_ad_duration=60000&rdid=ROKU_ADS_TRACKING_ID&is_lat=ROKU_ADS_LIMIT_TRACKING&idtype=rida&correlator=ROKU_ADS_TIMESTAMP&scor=ROKU_ADS_TIMESTAMP&pod=POD_NUM&ppos=POD_POSITION&cust_params=genre%3DROKU_ADS_CONTENT_GENRE%26ua%3DROKU_ADS_USER_AGENT%26ai%3DROKU_ADS_APP_ID]http://roku.com&unviewed_position_start=1&output=vast&impl=s&env=vp&gdfp_req=1&ad_rule=0&description_url=http%3A%2F%2Fapps.roku.com&vad_type=linear&sdkv=roku&min_ad_duration=0&max_ad_duration=60000&rdid=ROKU_ADS_TRACKING_ID&is_lat=ROKU_ADS_LIMIT_TRACKING&idtype=rida&correlator=ROKU_ADS_TIMESTAMP&scor=ROKU_ADS_TIMESTAMP&pod=POD_NUM&ppos=POD_POSITION&cust_params=genre%3DROKU_ADS_CONTENT_GENRE%26ua%3DROKU_ADS_USER_AGENT%26ai%3DROKU_ADS_APP_ID")
adPods = raf.getAds()
shouldPlayContent = raf.showAds(adPods)
然而,我不想在启动时播放广告,我想在视频播放之前播放广告。所以我想我可以把上面的行添加到components/screens/DetailsScreen/DetailsScreen.brs
:
' on Button press handler
Sub onItemSelected()
' first button is Play
if m.top.itemSelected = 0
'START ADDING FOR PRE-ROLL
raf = Roku_Ads()
raf.setAdPrefs(false) 'disable back-filled ads
raf.setDebugOutput(true)
raf.setAdUrl("http://pubads.g.doubleclick.net/gampad/ads?slotname=/82114269/Video/fallbacktest&sz=1920x1080&url=[http://roku.com&unviewed_position_start=1&output=vast&impl=s&env=vp&gdfp_req=1&ad_rule=0&description_url=http%3A%2F%2Fapps.roku.com&vad_type=linear&sdkv=roku&min_ad_duration=0&max_ad_duration=60000&rdid=ROKU_ADS_TRACKING_ID&is_lat=ROKU_ADS_LIMIT_TRACKING&idtype=rida&correlator=ROKU_ADS_TIMESTAMP&scor=ROKU_ADS_TIMESTAMP&pod=POD_NUM&ppos=POD_POSITION&cust_params=genre%3DROKU_ADS_CONTENT_GENRE%26ua%3DROKU_ADS_USER_AGENT%26ai%3DROKU_ADS_APP_ID]http://roku.com&unviewed_position_start=1&output=vast&impl=s&env=vp&gdfp_req=1&ad_rule=0&description_url=http%3A%2F%2Fapps.roku.com&vad_type=linear&sdkv=roku&min_ad_duration=0&max_ad_duration=60000&rdid=ROKU_ADS_TRACKING_ID&is_lat=ROKU_ADS_LIMIT_TRACKING&idtype=rida&correlator=ROKU_ADS_TIMESTAMP&scor=ROKU_ADS_TIMESTAMP&pod=POD_NUM&ppos=POD_POSITION&cust_params=genre%3DROKU_ADS_CONTENT_GENRE%26ua%3DROKU_ADS_USER_AGENT%26ai%3DROKU_ADS_APP_ID")
adPods = raf.getAds()
shouldPlayContent = raf.showAds(adPods)
'END ADDING FOR PRE-ROLL
if shouldPlayContent
m.videoPlayer.visible = true
m.videoPlayer.setFocus(true)
m.videoPlayer.control = "play"
m.videoPlayer.observeField("state", "OnVideoPlayerStateChange")
end if
end if
End Sub
当我运行这个时,我得到一个错误:
Roku_Ads Framework version 1.8
BRIGHTSCRIPT: ERROR: roAppInfo: class PLUGIN|MARKUP on thread RENDER: roku_ads_lib:/Roku_Ads.brs(527)
'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in roku_ads_lib:/Roku_Ads.brs(528)
528: ??
Backtrace:
#4 Function roku_ads_util_getappid(key_ As String) As String
file/line: roku_ads_lib:/Roku_Ads.brs(528)
#3 Function roku_ads_checkallowedfeature(keytype_ As String, util_ As Object) As Boolean
file/line: roku_ads_lib:/Roku_Ads.brs(1287)
#2 Function roku_ads_constructor() As Object
file/line: roku_ads_lib:/Roku_Ads.brs(264)
#1 Function roku_ads() As Object
file/line: roku_ads_lib:/Roku_Ads.brs(32)
#0 Function onitemselected() As Void
file/line: pkg:/components/screens/DetailsScreen/DetailsScreen.brs(75)
我在这里做错了什么?当从component.xml中的onChange="onItemSelected"
调用方法时,您可以不播放广告吗?
更新: Eugene下面的答案是正确的,下面是我如何解决这个问题的:
阅读场景图形线程,事件和处理应用程序事件。一旦完成,阅读这篇关于在渲染线程和主线程之间传递数据的文章。您需要了解场景图形数据范围。
基本上你观察components/screens/DetailsScreen/DetailsScreen.xml
中的itemSelected
字段:
m.DetailsScreen = scene.findNode("DetailsScreen")
m.DetailsScreen.observeField("itemSelected", m.port)
然后在主线程事件循环中处理roSGNodeEvent
:
while true
msg = wait(0, m.port)
msgType = type(msg)
? ">>>Main Thread Msg: ";msgType
if msgType = "roSGScreenEvent"
if msg.isScreenClosed() then return
else if msgType = "roSGNodeEvent"
fieldName = msg.getField()
fieldData = msg.getData()
? "> node: "; msg.getNode()
? "> field name: "; fieldName
? "> data: "; fieldData
if (fieldName = "itemSelected" and fieldData = 0) 'User pressed play
'wait for 0.5 second before proceeding to RAF
sleep(500)
PlayAd()
endif
end if
end while
如果没有广告或者广告播放完毕,PlayAd()
设置m.DetailsScreen.videoPlayerVisible = true
。这会导致onVideoVisibleChange
在components/screens/DetailsScreen/DetailsScreen.brs
你不能在渲染线程中使用RAF。你必须把事件发送到主线程然后在那里显示广告。你可以在这里的场景图中找到RAF集成的示例。关于场景图中线程的更多信息可以在这里找到