neowinston
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
06:00 AM
How to stop the audio with any key pressed on Remote Control?
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")
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]
5 REPLIES 5
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
10:32 AM
Re: How to stop the audio with any key pressed on Remote Control?
"neowinston" wrote: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().
neowinston
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
11:28 AM
Re: How to stop the audio with any key pressed on Remote Control?
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
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
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
12:22 PM
Re: How to stop the audio with any key pressed on Remote Control?
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
neowinston
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
01:17 PM
Re: How to stop the audio with any key pressed on Remote Control?
Thank you! That solved the problem!
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2017
01:33 PM
Re: How to stop the audio with any key pressed on Remote Control?
"neowinston" wrote:
Thank you! That solved the problem!
Always glad to see a new experienced developer come to the Roku platform! 8-)