Audio Toggle On/Off Button

Hello,

I am fairly new to this forum and entirely new to Corona and Lua altogether, and I’ve done several searches, and I think I saw that it was impossible, but wasn’t sure if it was the same thing I am asking…

My question is, is it possible to create a button that can toggle on/off all audio my game is playing.

and if you could supply a sample code if it is…Thanks and I’d really appreciate it. =] [import]uid: 14940 topic_id: 6159 reply_id: 306159[/import]

you could do that via using global variable.

for example:

local soundOn = true

if the button pressed then change it to false.

and before you play any sound just check

[lua]if soundOn == true then
sound.play()
end[/lua]

[import]uid: 22024 topic_id: 6159 reply_id: 21133[/import]

Thanks for the response!

But, I’m not entirely sure how to set that up =\ [import]uid: 14940 topic_id: 6159 reply_id: 21160[/import]

if you post your code here i can give you a clue

p.s. use [/lua] tags between your code [import]uid: 22024 topic_id: 6159 reply_id: 21164[/import]

Ok, if I wanted to use this button to stop all sound in my game when pressed, how would I set up the code to give it that action

 local onTogglePauseTouch = function( event )  
 if event.phase == "release" and pauseToggleBtn.isActive then  
  
  
 audio.play( tapSound )  
  
 end  
 end  
  
  
  
  
  
 pauseToggleBtn = ui.newButton{  
 defaultSrc = "togglebtn.png",  
 defaultX = 44,  
 defaultY = 44,  
 overSrc = "togglebtn-over.png",  
 overX = 44,  
 overY = 44,  
 onEvent = onTogglePauseTouch,  
 id = "PauseToggleButton",  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 pauseToggleBtn.x = 38; pauseToggleBtn.y = 100  
 pauseToggleBtn.isVisible = false  
 pauseToggleBtn.isActive = false  

Thanks! [import]uid: 14940 topic_id: 6159 reply_id: 21167[/import]

check this link to stop sounds: http://developer.anscamobile.com/reference/index/audiostop

but in this code you are playing sound instead of stopping?

[lua]
local onTogglePauseTouch = function( event )
if event.phase == “release” and pauseToggleBtn.isActive then
audio.play( tapSound )
end
end[/lua]

if you want to stop music and dont play anymore if this button pressed you should use variable like i said previous post.

for example:

[lua]local soundOn = true

local onTogglePauseTouch = function( event )
if event.phase == “release” and pauseToggleBtn.isActive then
soundOn = false
else
soundOn = true
end
end
–when you play sound check it
if soundOn == true then
audio.play( soundObject )
end[/lua]
[import]uid: 22024 topic_id: 6159 reply_id: 21171[/import]

As he notes, it’s a completely different situation if you want to stop the sounds vs. pause them vs. mute them. Read through the audio api:
http://developer.anscamobile.com/reference/audio

Note that there are separate commands for stop, pause, and setVolume. [import]uid: 12108 topic_id: 6159 reply_id: 21177[/import]