[Resolved] Audio Not Resuming

Hello,
I added some audio into my game, Hammy’s Harvest but I can’t get it to resume. I’m not sure if there is something going on with the channels but everything else seems to be working…

local sound = {  
 on = display.newImageRect("sound\_on.png", 75, 68),  
 off = display.newImageRect("sound\_off.png", 75, 68),  
}  
sound.on.x, sound.on.y = 40, 440  
sound.off.x, sound.off.y = 40, 440  
  
button\_sound = audio.loadSound("pop.wav")  
--I added the code below because I thought that it would help performance. Any advice?  
music\_channel = audio.loadStream("Soundtrack.mp3")  
  
--I got rid of the extra button because I wanted to just make one button for the SFX and Soundtrack  
--[[local music = {  
 on = display.newImageRect("music\_on.png", 75, 68),  
 off = display.newImageRect("music\_off.png", 75, 68),  
}  
music.on.x, music.on.y = 100, 440  
music.off.x, music.off.y = 100, 440  
--]]  
  
--music\_channel = audio.loadSound("Soundtrack.mp3")  
music\_channel = audio.play( music\_channel, { channel=1, loops=-1, fadein=5000 } )  
--play\_sound(music\_channel)  
local function set\_btn\_states()  
 if settings.game.options.sound then  
 sound.on.isVisible = true sound.off.isVisible = false  
 else  
 sound.on.isVisible = false sound.off.isVisible = true  
 end  
 end  
  
set\_btn\_states()  
--I tore down this code from what it was before only because it wasn't working but if you see anything that would fix it let me know. How this code looked before I tore it down is below in the next section.  
local function switch\_state(event)  
 if event.phase == "ended" then  
 if event.target==sound.on then  
 audio.pause( music\_channel )  
 print("First State Working")  
 end  
 if event.target==sound.off then  
 audio.resume( music\_channel )  
 print("Second State Working")  
 end  
 -- this is for the pop sound   
 set\_btn\_states()  
 settings:save()  
--audio.resume( { channel = 1 } )  
 play\_sound(button\_sound)  
 end  
 end  
  
sound.on:addEventListener("touch", switch\_state)  
sound.off:addEventListener("touch", switch\_state)  

Pre torn down…

local sound = {  
 on = display.newImage("sound\_on.png"),  
 off = display.newImage("sound\_off.png"),  
}  
sound.on.x, sound.on.y = 250, 30  
sound.off.x, sound.off.y = 250, 30  
  
local music = {  
 on = display.newImage("music\_on.png"),  
 off = display.newImage("music\_off.png"),  
}  
music.on.x, music.on.y = 300, 30  
music.off.x, music.off.y = 300, 30  
  
localGroup:insert(sound.on)  
localGroup:insert(sound.off)  
localGroup:insert(music.on)  
localGroup:insert(music.off)  
  
button\_sound = audio.loadSound("button.wav")  
  
local function set\_btn\_states()  
 if settings.game.options.sound then  
 sound.on.isVisible = true sound.off.isVisible = false  
 else  
 sound.on.isVisible = false sound.off.isVisible = true  
 end  
 if settings.game.options.music then  
 music.on.isVisible = true music.off.isVisible = false  
 else  
 music.on.isVisible = false music.off.isVisible = true  
 end  
end  
set\_btn\_states()  
  
--[[  
DISCOMMENT FOLLOWING LINES IF YOU PLAN TO USE MUSIC  
  
local music\_channel = audio.play("music.mp3", {loops=-1})  
  
if not settings.game.options.music then  
 audio.pause(music\_channel)  
end  
]]  
  
local function switch\_state(event)  
 if event.phase == "ended" then  
 if event.target==sound.on or event.target==sound.off then  
 settings.game.options.sound = not settings.game.options.sound  
 else  
 settings.game.options.music = not settings.game.options.music  
 if not settings.game.options.music then   
 audio.pause(music\_channel)  
 else  
 audio.resume(music\_channel)  
 end  
 end  
 set\_btn\_states()  
 settings:save()  
 play\_sound(button\_sound)  
 end  
end  
sound.on:addEventListener("touch", switch\_state)  
sound.off:addEventListener("touch", switch\_state)  
music.on:addEventListener("touch", switch\_state)  
music.off:addEventListener("touch", switch\_state)  
  
return localGroup  
end  

Thanks for checking it out, I hope this doesn’t look daunting I just wanted to show everything.

Danny [import]uid: 59140 topic_id: 27019 reply_id: 327019[/import]

Note that the audio APIs can be very fussy about whether you are calling a HANDLE or a CHANNEL. Sometimes you must use exactly the right one, otherwise the API might not work (I say “might” because it can be unpredictable). Which one to point to in your calls is detailed in the API docs.

“loadStream” returns a handle, not a channel. “audio.pause” and “audio.resume” take a channel as their argument, so you probably can’t pass the handle. Again, sometimes you can use either, but not always. Try out both and see what happens.

Also, your audio pointers probably shouldn’t be global. This might be creating other problems, or might even be the root issue in all of this. EDIT: in fact, I see you declared “music-channel” twice, on both loadStream and play. Ensure this is the same local variable in each call.

Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 27019 reply_id: 109635[/import]

I have similar problem to this one. Audio channel 1 is reserved to play background music using local stream handle.

I found when time between resume and suspend is LONGER then total length of stream which is played before suspend then it does not bring it back after resume.

I also found that it can block sounds in other apps (especially made in Corona but by other developer).

Tom [import]uid: 111283 topic_id: 27019 reply_id: 109640[/import]

@ Bladko (Tom),

Strange behavior… this sounds like a bug. Have you submitted an official bug report for this so Ansca can take a look? Audio streams shouldn’t be behaving like that. :stuck_out_tongue:

Brent Sorrentino
[import]uid: 9747 topic_id: 27019 reply_id: 109645[/import]

The audio APIs DEMAND you pass the correct parameter. If you pass the wrong one, you should expect it to fail or crash. If it works, you are unlucky because it may not work next time.

load* returns a handle.
play always takes a handle (which starts playing a sound on an available channel)
Everything thing else generally deals with that channel.
(Exception: rewind/seek depend on whether it was loadSound or loadStream.)
Bladko: for the resume/suspend, you should file a bug with a simple reproducible test case and explain which platforms this happens on.

Also, iOS 5 changed something so multiple OpenAL apps suspending/resuming could crash Apple’s audio daemon causing sound to stop playing in all those apps. We already fixed that a number of weeks ago in a daily build.
[import]uid: 7563 topic_id: 27019 reply_id: 109647[/import]

WOW talk about unpredictable! I changed “music_channel” to “musicChannel” and all of the sudden it started working again. Is that a bug I should submit? Now it is working great though and thanks for the info. I had no idea that channels and handles could get intertwined like that.

Danny [import]uid: 59140 topic_id: 27019 reply_id: 109828[/import]

No bug involved here! And I retract my comment about the audio APIs being “unpredictable”. They are just VERY specific, as Eric points out, and you can’t just haphazardly toss channels and handles at the various function calls and expect it to work. Really, there’s no reason we should expect that… we can’t just toss sloppy Lua code into a cleanup function, for example, and expect Lua to figure out what we intend. :slight_smile:

Brent Sorrentino
[import]uid: 9747 topic_id: 27019 reply_id: 109835[/import]