Music muting.

Hi,

I load my music with audio.loadStream and play it back with audio.play but I need to pause the music for a second and then play it back. I delved into Corona docs and got a bit confused and wanted to ask you guys if I’m doing the right thing by calling audio.stop() and then un-pausing by calling audio.play on same audio handle.

I tested it and it actually un-paused the track, even though I called “audio.stop()” upon it, so I’m kinda suspicious if it’s right to do.

Thanks.

I think I’m doing the right thing:

For audio loaded with audio.loadStream(), the position of the audio will not be reset when stop is called. So when if you call audio.play()
again on the same handle, it will continue approximamotely where it left
off (approximately meaning it might be a little further down because all
the pre-filled buffers were discarded on the audio.stop()). This contrasts with audio loaded with audio.loadSound()
which will start playing at the beginning of the starting position of
the buffer (e.g. beginning of the sound unless you modified it with audio.seek()). So for audio.loadStream, you may want to call audio.rewind() on the handle if you reuse the audio handle.

It would be nice if someone one with experience on this would verify or shed some light on this.

Thanks.

What do you mean by “play the music for a second then play it back”?  You want the music to start over from the beginning?  Or you want to resume where it left off?

Did I say that?! Let me quote myself:

Hi,

I load my music with audio.loadStream and play it back with audio.play
but I need to pause the music for a second and then play it back. I
delved into Corona docs and got a bit confused and wanted to ask you
guys if I’m doing the right thing by calling audio.stop() and then
un-pausing by calling audio.play on same audio handle.

I
tested it and it actually un-paused the track, even though I called
“audio.stop()” upon it, so I’m kinda suspicious if it’s right to do.

Thanks.

So what I want is to pause the music playback and then be able to resume the playback from where it was paused.

Hi Aidin,

Why not use “audio.pause()”?

http://docs.coronalabs.com/api/library/audio/pause.html

Brent

Thanks Brent,

Because of that documentation code I quoted earlier here. I tried to do pause and it does pause but it won’t turn the music back on when I call audio.play on same audioHandle. Please note that I load my music as stream.

Here is my pause and un-pause code:

sfx.playMusic = function() -- TODO: Just did it fast and dirty, read Corona documentation later as I think I need to clean this up later on. if not musicHandle then musicHandle = audio.loadStream( "music.mp3" ) end local musicChannel = audio.play( musicHandle, {channel = musicChannel, loops=-1} ) end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- sfx.pauseMusic = function() -- audio.stop() -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. audio.pause(musicHandle) -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. end

 

Hi Aidin,

You can’t pause audio based on a handle… you need to pause a channel (and resume, stop, etc.). The difference between handles and channels is documented in the guide:

http://docs.coronalabs.com/guide/media/audioSystem/index.html

Take care,

Brent

Also, if you use audio.pause then you need to use audio.resume (not audio.play) to continue

http://docs.coronalabs.com/api/library/audio/index.html

Thanks Brent, I think I did that in my code posted before.

Thanks, I think it’s better now:

sfx.playMusic = function() -- TODO: Just did it fast and dirty, read Corona documentation later as I think I need to clean this up later on. if not musicHandle then musicHandle = audio.loadStream( "music.mp3" ) end --------- if musicPlaybackHandle then musicPlaybackHandle = audio.resume(musicChannel) else musicPlaybackHandle = audio.play( musicHandle, {channel = musicChannel, loops=-1} ) end end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- sfx.pauseMusic = function() -- audio.stop() -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. audio.pause(musicChannel) -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. end

I think I’m doing the right thing:

For audio loaded with audio.loadStream(), the position of the audio will not be reset when stop is called. So when if you call audio.play()
again on the same handle, it will continue approximamotely where it left
off (approximately meaning it might be a little further down because all
the pre-filled buffers were discarded on the audio.stop()). This contrasts with audio loaded with audio.loadSound()
which will start playing at the beginning of the starting position of
the buffer (e.g. beginning of the sound unless you modified it with audio.seek()). So for audio.loadStream, you may want to call audio.rewind() on the handle if you reuse the audio handle.

It would be nice if someone one with experience on this would verify or shed some light on this.

Thanks.

What do you mean by “play the music for a second then play it back”?  You want the music to start over from the beginning?  Or you want to resume where it left off?

Did I say that?! Let me quote myself:

Hi,

I load my music with audio.loadStream and play it back with audio.play
but I need to pause the music for a second and then play it back. I
delved into Corona docs and got a bit confused and wanted to ask you
guys if I’m doing the right thing by calling audio.stop() and then
un-pausing by calling audio.play on same audio handle.

I
tested it and it actually un-paused the track, even though I called
“audio.stop()” upon it, so I’m kinda suspicious if it’s right to do.

Thanks.

So what I want is to pause the music playback and then be able to resume the playback from where it was paused.

Hi Aidin,

Why not use “audio.pause()”?

http://docs.coronalabs.com/api/library/audio/pause.html

Brent

Thanks Brent,

Because of that documentation code I quoted earlier here. I tried to do pause and it does pause but it won’t turn the music back on when I call audio.play on same audioHandle. Please note that I load my music as stream.

Here is my pause and un-pause code:

sfx.playMusic = function() -- TODO: Just did it fast and dirty, read Corona documentation later as I think I need to clean this up later on. if not musicHandle then musicHandle = audio.loadStream( "music.mp3" ) end local musicChannel = audio.play( musicHandle, {channel = musicChannel, loops=-1} ) end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- sfx.pauseMusic = function() -- audio.stop() -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. audio.pause(musicHandle) -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. end

 

Hi Aidin,

You can’t pause audio based on a handle… you need to pause a channel (and resume, stop, etc.). The difference between handles and channels is documented in the guide:

http://docs.coronalabs.com/guide/media/audioSystem/index.html

Take care,

Brent

Also, if you use audio.pause then you need to use audio.resume (not audio.play) to continue

http://docs.coronalabs.com/api/library/audio/index.html

Thanks Brent, I think I did that in my code posted before.

Thanks, I think it’s better now:

sfx.playMusic = function() -- TODO: Just did it fast and dirty, read Corona documentation later as I think I need to clean this up later on. if not musicHandle then musicHandle = audio.loadStream( "music.mp3" ) end --------- if musicPlaybackHandle then musicPlaybackHandle = audio.resume(musicChannel) else musicPlaybackHandle = audio.play( musicHandle, {channel = musicChannel, loops=-1} ) end end ---------------------------------------------------------------------------------------- -- ---------------------------------------------------------------------------------------- sfx.pauseMusic = function() -- audio.stop() -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. audio.pause(musicChannel) -- According to Corona docs, calling this on a loadStream doesn't stop, but pause it. end