[SOLVED] Music, Sound Mute with audio.setVolume()

This seems to be so easy, but I’m stuck here.

What I want to accomplish is a seperate On/Off state for both Music and Sound. The user should be able to switch it ingame.
Right now it works on the simulator, but not on the actual device (iPad). It’s probably a bug as other users have mentioned on the API page.
http://developer.coronalabs.com/reference/index/audiosetvolume
I’ve read similar topics and mostly the solution was an if statement before every audio.play but that’s over the top.

[code]

local isMusicOn = true

audio.reserveChannels( 2 ) – reserve channels 1 and 2 for music only

local music_channel = audio.loadStream(“bg.mp3”) – 3 min background music
local swoshSound = audio.loadSound(“swosh.caf”) – 1s Sound

bg_music = audio.play( music_channel, { channel=1, loops=-1, fadein=5000 } )
timer.performWithDelay( 1000, function () audio.play(swoshSound) end , 0 ) – loop sound for testing

local function sound_switch(state)
if state == “off” then
print(“sound off”)
audio.setMaxVolume( 0 ) – mute all channels

if isMusicOn == true then
audio.setMaxVolume( 1, { channel=1 } ) – keeps music on
audio.setVolume( 1, { channel= 1 } )
end

else
audio.setMaxVolume( 1 )

audio.setVolume( 1)
print(“sound on”)

end
end

local function music_switch(state)
if state == “off” then
print(“music off”)
audio.setVolume( 0, { channel= 1 } ) --mute only music channel
isMusicOn = false
else
isMusicOn = true
audio.setMaxVolume( 1, { channel=1 } ) – keeps music on
audio.setVolume( 1, { channel= 1 } )
print(“music on”)

end
end

– … more code, irrelevant for support …
[/code] [import]uid: 98393 topic_id: 29145 reply_id: 329145[/import]

Yay, I’ve found something that works. Why is it that I mostly find a solution right after posting a thread :smiley:

for i=2,32 do  
 audio.setVolume(1.0, {channel=i})  
end  

But setmaxvolume still doesn’t work. Could be neat for fine-tuning. [import]uid: 98393 topic_id: 29145 reply_id: 117223[/import]

Isn’t that always the way? :wink:

Thanks very much for sharing your solution.

To confirm, do you believe there’s a bug with setMaxVolume? [import]uid: 52491 topic_id: 29145 reply_id: 117285[/import]

I’m thinking this may be related to an Apple issue - did you read the comments here; http://developer.coronalabs.com/reference/index/audiosetmaxvolume ?

I need to chase this up with Eric, he’s the audio guru :wink: [import]uid: 52491 topic_id: 29145 reply_id: 117853[/import]

Yes, there’s a bug. It only works in the simulator. Isn’t setMaxVolume basically just:

  
local V\_MAX = 0.7  
  
local function setVolume(volume)  
 if volume \< V\_MAX then  
 audio.setVolume(volume})  
 else  
 audio.setVolume(V\_MAX})  
end  
  
setVolume(0.8) -- Volume 0.7  
setVolume(0.6) -- Volume 0.6  
  

? [import]uid: 98393 topic_id: 29145 reply_id: 117747[/import]