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

Audio playback in the background

Hi guys,

I'm fairly new to Roku development, been playing with it only for a couple of days.

I'm trying to get the audio player to play in the background. Right now I have a springboard - while loading it, I also start playing. I'd like users that click the 'Back' button, to go to the previous screen, but, without the player stopping.

Right now all I have is a main screen (poster screen), the springboard with audio metadata and player - in that screen, I simply initialize everything and run the while loop that waits for events. Once I get the "isScreenClosed" event on "roSpringboardScreenEvent"

I simply exit the while (so I will be able to listen to events from the previous view) - that's when audio stops.

Any ideas how to do this?

Thanks guys!



I initialize the player like this:

Function initAudio() As Object
o = CreateObject("roAssociativeArray")

o.isPlayState = 0 ' Stopped
o.setPlayState = audioPlayer_newstate
o.setupSong = audioPlayer_setup
o.clearContent = audioPlayer_clear_content
o.setContentList = audioPlayer_set_content_list
o.getMsgEvents = audioPlayer_getmsg

audioPlayer = CreateObject("roAudioPlayer")
o.port = CreateObject("roMessagePort") 'Message will be coming out when a track is done
audioPlayer.SetMessagePort(o.port)
o.audioPlayer = audioPlayer

audioPlayer.SetLoop(0)

m.Audio = o
return o
End Function




And I listen to the events like this:

while true
msg = m.Audio.getMsgEvents(20000, "roSpringboardScreenEvent")

if type(msg) = "roAudioPlayerEvent" then ' event from audio player
if msg.isStatusMessage() then
message = msg.getMessage()
print "AudioPlayer Status Event - " message
if message = "end of playlist"
print "end of playlist (obsolete status msg event)"
' ignore
else if message = "end of stream"
print "done playing this song (obsolete status msg event)"
'audio.setPlayState(0) ' stop the player, wait for user input
'scr.ReloadButtons(0) ' set button to allow play start
endif
else if msg.isListItemSelected() then
print "starting song:"; msg.GetIndex()
else if msg.isRequestSucceeded()
print "ending song:"; msg.GetIndex()
m.Audio.setPlayState(0) ' stop the player, wait for user input
scr.ReloadButtons(0) ' set button to allow play start
else if msg.isRequestFailed()
print "failed to play song:"; msg.GetData()
else if msg.isFullResult()
print "FullResult: End of Playlist"
else if msg.isPaused()
print "Paused"
else if msg.isResumed()
print "Resumed"
else
print "ignored event type:"; msg.getType()
endif
else if type(msg) = "roSpringboardScreenEvent" then ' event from user
if msg.isScreenClosed()
print "Show_Audio_Screen: screen close - return"

exit while
endif
if msg.isRemoteKeyPressed() then
button = msg.GetIndex()
print "Remote Key button = "; button
else if msg.isButtonPressed() then
button = msg.GetIndex()
print "button index="; button
if button = 1 'pause or resume
if m.Audio.isPlayState < 2 ' stopped or paused?
if (m.Audio.isPlayState = 0)
m.Audio.audioplayer.setNext(0)
endif
newstate = 2 ' now playing
else 'started
newstate = 1 ' now paused
endif
else if button = 2 ' stop
newstate = 0 ' now stopped
endif
m.Audio.setPlayState(newstate)
scr.ReloadButtons(newstate)
scr.Show()
endif
endif
end while
0 Kudos