How to make a song play continuously in a specific scene .

I want to make a song in my game file called app music play when I come to the scene . And also how do I make a button in my restart.lua file that makes that music be able to turn on and off .

Thank you .

Just a quick forum etiquette note before I answer…  It’s always best to ask questions on different topics in different threads.  In this  case making a button to turn your music on and off is different than how to make your music loop. Which leads to the second point, the title of your post should accurately portrait the information you seek. The title asks how do you loop music. The post asks how to play music when you come into your scene.

The reason this is important is that many people search the forums for answers. When the title of the thread has something to do with what they are searching for, they will click through and read the post. When the click and the post is about something else, they get frustrated because it didn’t answer their question and they end up making a new post and making it harder to find answers.

In fact all three questions you’re asking have been answered multiple times and searching should have provided the answers.

Now to the answers.

  1. How to loop music:  See the audio.play documentation here:  https://docs.coronalabs.com/api/library/audio/play.html

Set the value for “loop” in the options table to -1 and the track will repeat. You want to store the handle to the sound in a variable (probably declared at the top of the scene module since you will likely be in a different function when you’re ready to stop the sound.)

  1. How to start music when you enter the scene:  See the audio.play documentation linked above;

In scene:show()'s “did” phase, call audio.play() with the appropriate parameters. Since you will be calling the stop function for this sound elsewhere, it’s best to create a local variable at the top of the scene file.

2b. How to stop music when leaving the scene:

In scene:hide()'s “will” phase, call audio.stop() passing in the handle to the audio track as the parameter to audio.stop().

  1. How to make a button to stop/play audio: The button is pretty straight forward. You can use widget.newButton() for it.  Create it in scene:create() in your reset.lua scene. But the logic is a bit more complex.  First you need a setting variable that is globally visible. We recommend this tutorial:

https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

You create a simple data table that you require in all scenes.  One of the members of that table is your music On/Off setting.  You can learn how to do all of this if you look at our Game Sample Project on Github:  https://github.com/coronalabs/sample-game-project

Looking at the gamesettings.lua file, the mydata.lua file should have all of these pieces. I used a widget.newSwitch and a display.newText() in the project instead of a button.

The one thing it’s missing is playing the audio itself. If you follow the code in that project, you then need to wrap all of your audio calls in an if statement to test if the switch is on. Many games separate music and sound effects onto different switches.  So for your music, you would do something like:

if mydata.musicOn then

     soundtrackHandle = audio.play( … )

end

where soundtrackHandle is a variable you declared at the top of the scene.

Rob

Just a quick forum etiquette note before I answer…  It’s always best to ask questions on different topics in different threads.  In this  case making a button to turn your music on and off is different than how to make your music loop. Which leads to the second point, the title of your post should accurately portrait the information you seek. The title asks how do you loop music. The post asks how to play music when you come into your scene.

The reason this is important is that many people search the forums for answers. When the title of the thread has something to do with what they are searching for, they will click through and read the post. When the click and the post is about something else, they get frustrated because it didn’t answer their question and they end up making a new post and making it harder to find answers.

In fact all three questions you’re asking have been answered multiple times and searching should have provided the answers.

Now to the answers.

  1. How to loop music:  See the audio.play documentation here:  https://docs.coronalabs.com/api/library/audio/play.html

Set the value for “loop” in the options table to -1 and the track will repeat. You want to store the handle to the sound in a variable (probably declared at the top of the scene module since you will likely be in a different function when you’re ready to stop the sound.)

  1. How to start music when you enter the scene:  See the audio.play documentation linked above;

In scene:show()'s “did” phase, call audio.play() with the appropriate parameters. Since you will be calling the stop function for this sound elsewhere, it’s best to create a local variable at the top of the scene file.

2b. How to stop music when leaving the scene:

In scene:hide()'s “will” phase, call audio.stop() passing in the handle to the audio track as the parameter to audio.stop().

  1. How to make a button to stop/play audio: The button is pretty straight forward. You can use widget.newButton() for it.  Create it in scene:create() in your reset.lua scene. But the logic is a bit more complex.  First you need a setting variable that is globally visible. We recommend this tutorial:

https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

You create a simple data table that you require in all scenes.  One of the members of that table is your music On/Off setting.  You can learn how to do all of this if you look at our Game Sample Project on Github:  https://github.com/coronalabs/sample-game-project

Looking at the gamesettings.lua file, the mydata.lua file should have all of these pieces. I used a widget.newSwitch and a display.newText() in the project instead of a button.

The one thing it’s missing is playing the audio itself. If you follow the code in that project, you then need to wrap all of your audio calls in an if statement to test if the switch is on. Many games separate music and sound effects onto different switches.  So for your music, you would do something like:

if mydata.musicOn then

     soundtrackHandle = audio.play( … )

end

where soundtrackHandle is a variable you declared at the top of the scene.

Rob