2 audio files on the same channel

–main.lua

audio.reserveChannels(1)

–scene1

–In scene:create

bgMusic = audio.loadStream(“audio1.mp3”)

–In scene:show

audio.play(bgMusic, {channel=1})

   

–In scene:hide 

audio.stop(1) 

–scene2

-In scene:create

bgMusic = audio.loadStream(“audio2.mp3”)

–In scene:show

audio.play(bgMusic, {channel=1})

     

If I try to play 2 different audio files in the same scene on the same channel using play() stop() play() it does the work but not when I’m using composer and 2 scenes. Why is that? 

You might have to dispose the audio for one of the scenes

Please note, the way you imply you’re using show() and hide() is also wrong.

Those have will and did phases, so if you have code that affects the channel in:

  • scene 1 hide()
  • scene2 show()

You’ll get this sequence:

  1. scene 1 will hide() - audio.stop()
  2. scene 2 will show() - audio.loadStream()
  3. scene 1 did  hide() - audio.stop()
  4. scene 2 did show() - audio.loadStream()

If these all end up getting processed in the same frame, you may have varied results.  

In any case, you need to pay attention to the will and did phases of show()/hide().

https://docs.coronalabs.com/api/event/scene/show/phase.html

https://docs.coronalabs.com/api/event/scene/hide/phase.html

That’s it .I just needed to use scene phases differently. But the sequence by which scenes are executed is not clear to me.

Looks like scene 2 create() comes before scene 2 show() either is  will or  did. But the scene 2 create() also probably comes after previous scene 1 hide()  will or  did. How scene 1  did  hide() can be executed at the same time as the scene 2  will  show(). Where the scene create() stands here?

If you want to work this out, just put print statements in all the right places on two scenes, then look at the logs to see what prints out.

Note: This info is also on the site, I just don’t remember where, but I found the image which explains it by googling ‘composer flowchart’:

composer-flowchart.png

The flowchart and its accompanying tutorials and explanations can be found at https://docs.coronalabs.com/guide/system/composer/index.html