I’m new to Roku and this is my first post. I need to play .m4a files and I’m using this code:
audioPlayer = CreateObject(“roAudioPlayer”)
port = CreateObject(“roMessagePort”)
port = CreateObject(“roUniversalControlEvent”)
audioPlayer.SetMessagePort(port)
song = CreateObject(“roAssociativeArray”)
song.url = audioURL
audioplayer.addcontent(song)
audioplayer.setloop(false)
audioPlayer.play()
[size=85]It works great, playing the audio files. Now I need to stop the audio with any key pressed on the Remote Control. How do I do that? Thank you![/font][/size]
audioPlayer = CreateObject("roAudioPlayer")
port = CreateObject("roMessagePort")
port = CreateObject("roUniversalControlEvent") ' <-- wrong '
audioPlayer.SetMessagePort(port) ' <-- should have err'ed on wrong argument
This is not how you do it. The next line should have caused runtime error, trying to set message port into audioPlayer that is NOT a roMessagePort - but SDK APIs notoriously lax on type checking.
The way it works is the component you have on screen, be it roSpringboardScreen, roListScreen, roPosterScreen etc - it will send a ro…Event to its assigned port, which will respond with True to .isRemoteKeyPressed(). And then you can call .pause()/.resume() on the roAudioPlayer().
Thanks for your reply and for helping me. Do I need to include the to handle the remote control here? If so, how?
while true
msg = wait(0, screen.GetMessagePort())
if type(msg) = “roPosterScreenEvent” then
print "showPosterScreen | msg = "; msg.GetMessage() " | index = "; msg.GetIndex()
if msg.isListFocused() then
'get the list of shows for the currently selected item
screen.SetContentList(getShowsForCategoryItem(categoryList[msg.GetIndex()]))
print "list focused | current category = "; msg.GetIndex()
else if msg.isListItemFocused() then
print"list item focused | current show = "; msg.GetIndex()
else if msg.isListItemSelected() then
print "list item selected | current show = "; msg.GetIndex()
m.curShow = m.URLsList[msg.GetIndex()]
playOnDemandAudio(m.curShow.ProgramURL)
else if msg.isScreenClosed() then
return -1
end if
end If
end while
Something like this, adding yet another if-branch:
“neowinston” wrote:
if msg.isListFocused() then
...
else if msg.isRemoteKeyPressed() then
if msg.getIndex() < 100: ' i.e. button depressed'
theAudioPlayer.stop()
end if
end if