audio.play() not working after call to audio.pause()

I click a button to start music playing and then click to make it pause – when I click Play again I receive the following message in the terminal (and the sound does not start playing again):

Warning: audio error: Can’t play shared streamed sample because it is already in use

 

The code I’m using is below – can you spot what I’ve done wrong?

 

 Jay

 

display.setStatusBar(display.HiddenStatusBar) local widget = require("widget") audio.reserveChannels( 1 ) sndChanMusic = 1 local sndMusic = audio.loadStream ( "audio/electrohouse.wav" ) local function buttonHit(event) local action = event.target.id if action == "play" then event.target:setLabel("Pause") event.target.id = "pause" audio.play(sndMusic, {channel=sndChanMusic} ) elseif action == "pause" then event.target:setLabel("Play") event.target.id = "play" audio.pause ( sndChanMusic ) end end local btn = widget.newButton { label="Play", id = "play", fontSize = 30, defaultFile = "images/buttonDefault.png", overFile = "images/buttonSelected.png", onRelease=buttonHit, } btn.x = display.contentCenterX btn.y = display.contentCenterY

I’m wondering if the problem is due to using audio.loadStream – this is in the docs:

Unlike files loaded with audio.loadSound(), these cannot be shared simultaneously across multiple channels. If you need to play multiple simulataneous instances of the same file, you must load multiple instances of the file.

But I’m not wanting to play multiple instances, just restart the one already there.

 Jay

And, the problem is solved. If you pause the sound and want to resume, it, you need to use audio.resume instead of audio.play – it sounds so obvious now, of course. :slight_smile:

So this is the code that replaces the audio.play line in the above code:

 local isChanPaused = audio.isChannelPaused( sndChanMusic ) if isChanPaused then audio.resume( sndChanMusic ) else                   audio.play(sndMusic, {channel=sndChanMusic} ) end

Okay, thanks for everyone who helped me figure this out. :wink:

 Jay

I’m wondering if the problem is due to using audio.loadStream – this is in the docs:

Unlike files loaded with audio.loadSound(), these cannot be shared simultaneously across multiple channels. If you need to play multiple simulataneous instances of the same file, you must load multiple instances of the file.

But I’m not wanting to play multiple instances, just restart the one already there.

 Jay

And, the problem is solved. If you pause the sound and want to resume, it, you need to use audio.resume instead of audio.play – it sounds so obvious now, of course. :slight_smile:

So this is the code that replaces the audio.play line in the above code:

 local isChanPaused = audio.isChannelPaused( sndChanMusic ) if isChanPaused then audio.resume( sndChanMusic ) else                   audio.play(sndMusic, {channel=sndChanMusic} ) end

Okay, thanks for everyone who helped me figure this out. :wink:

 Jay

I found this to work with an added button graphic to pause and resume the music (which is on channel 27 in this example):

local function pause (event)

    audio.pause(27)

    audio.resume(27)

end

button:addEventListener( “tap”, pause )

I found this to work with an added button graphic to pause and resume the music (which is on channel 27 in this example):

local function pause (event)

    audio.pause(27)

    audio.resume(27)

end

button:addEventListener( “tap”, pause )