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: 
neowinston
Visitor

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]
0 Kudos
5 REPLIES 5
EnTerr
Roku Guru

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().
0 Kudos
neowinston
Visitor

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
0 Kudos
EnTerr
Roku Guru

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

0 Kudos
neowinston
Visitor

Re: How to stop the audio with any key pressed on Remote Control?

Thank you! That solved the problem!
0 Kudos
EnTerr
Roku Guru

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-)
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.