trying to toggle sound - no luck :\

Im trying to toggle my apps background music. So far i’ve got -

local musicBtn = display.newImageRect ("images/volumeOn.png", 100, 100) musicBtn.x = 100 musicBtn.y = 100 musicBtn:scale (1, 1) bgTune = audio.loadSound("music/backgroundMusic.mp3") audio.loop = -1 function musicBtn:tap (event) if bgTune then audio.play( bgTune ) else audio.stop( bgTune ) local musicBtn = display.newImageRect ("images/volumeOff.png", 100, 100) musicBtn.x = 100 musicBtn.y = 100 musicBtn:scale (1, 1) end musicBtn:addEventListener ("tap", musicBtn )

When I test in the simulator, I get a blank black screen. I dont know why it doesn’t work. Can anyone help me get this working? Any help is appreciated.  :D 

Not sure exactly why you’re getting a blank screen, I’m sure if you look at the terminal output it will give some indication.

I haven’t tested this, but I’d do something like this:

local isPlayingMusic = true local musicBtn = display.newImageRect ("images/volumeOn.png", 100, 100) musicBtn.x, musicBtn.y = 100, 100 --no need to call musicBtn:scale(1, 1), this is the default already local bgTune = audio.loadStream("music/backgroundMusic.mp3") audio.play( bgTune, {loops = -1} ) function musicBtn:tap (event) if isPlayingMusic then --if already playing, then stop music --and change the button image to "on" --so that it reflects what will happen when it is pushed again audio.stop( bgTune ) musicBtn.fill = { type="image", filename="images/volumeOn.png" } else audio.play( bgTune, {loops = -1} ) musicBtn.fill = { type="image", filename="images/volumeOff.png" } end --toggle the boolean isPlayingMusic = not isPlayingMusic end musicBtn:addEventListener ("tap", musicBtn )

A few pointers.

  1. When loading background music, you should generally use audio.loadStream instead of audio.loadSound to improve memory usage (loadSound will load the whole file into memory at once, loadStream will load it in smaller chunks as they are needed).

  2. I could be wrong but I don’t think that “audio.loop” does anything, you have to set the number of loops when calling audio.play()

  3. In your code, you created a local object in your tap function which had the exact same name as the original button object. This would not remove the old button, it would create a second button on top with the same name. The new button would also not have the tap listener. It would be advisable to look into how scope works in Lua.

Great! Thanks for the tips too - Im gonna try this out right now. B)

It appears to work until I click it - I will play around with it… Thanks. B)

Not sure exactly why you’re getting a blank screen, I’m sure if you look at the terminal output it will give some indication.

I haven’t tested this, but I’d do something like this:

local isPlayingMusic = true local musicBtn = display.newImageRect ("images/volumeOn.png", 100, 100) musicBtn.x, musicBtn.y = 100, 100 --no need to call musicBtn:scale(1, 1), this is the default already local bgTune = audio.loadStream("music/backgroundMusic.mp3") audio.play( bgTune, {loops = -1} ) function musicBtn:tap (event) if isPlayingMusic then --if already playing, then stop music --and change the button image to "on" --so that it reflects what will happen when it is pushed again audio.stop( bgTune ) musicBtn.fill = { type="image", filename="images/volumeOn.png" } else audio.play( bgTune, {loops = -1} ) musicBtn.fill = { type="image", filename="images/volumeOff.png" } end --toggle the boolean isPlayingMusic = not isPlayingMusic end musicBtn:addEventListener ("tap", musicBtn )

A few pointers.

  1. When loading background music, you should generally use audio.loadStream instead of audio.loadSound to improve memory usage (loadSound will load the whole file into memory at once, loadStream will load it in smaller chunks as they are needed).

  2. I could be wrong but I don’t think that “audio.loop” does anything, you have to set the number of loops when calling audio.play()

  3. In your code, you created a local object in your tap function which had the exact same name as the original button object. This would not remove the old button, it would create a second button on top with the same name. The new button would also not have the tap listener. It would be advisable to look into how scope works in Lua.

Great! Thanks for the tips too - Im gonna try this out right now. B)

It appears to work until I click it - I will play around with it… Thanks. B)