Disable sound when you touch a button (Mute?)

I’m trying to figure out how one would make a button that when pressed would mute codea and any sounds it may have! but can’t seem to find anything. Any ideas?

I think you will need to do it programmatically and keep a flag for if the user wants sound or not. I have not seen any master values like this, although there may be a metatable somewhere…

Codea doesn’t have a mute functionality, but you could override the sound function, and only call the original when a variable is set. Here is an example:


isSoundMuted = false

local _original_sound = sound
function sound(...)
    if not isSoundMuted then
        return _original_sound(...)
    end
end

function setup()
    
    -- play a sound after 1 second
    tween.delay(1, function()
        print("you should hear a sound")
        sound(SOUND_JUMP, 34360)        
    end)
    
    -- after 2 seconds, mute sound and try to play sound again
    tween.delay(2, function()
        isSoundMuted = true
        print("sound is muted, you should hear nothing")
        sound(SOUND_JUMP, 34360)
    end)
    
    -- after 3 seconds, unmute sound and try to play sound again
    tween.delay(3, function()
        isSoundMuted = false
        print("sound is unmuted, you should hear a sound")
        sound(SOUND_JUMP, 34360)
    end)

end

Basically, just set isSoundMuted to true, and any calls to sound() after that will not play a sound. Set it back to false and sounds will play again whenever the sound() function is called.

Note that this will not cancel any sounds that are already playing.

Or you could set a variable for the volume and set this to zero when you want to mute


vol=10 --set this to zero to mute as required


sound(SOUND_JUMP, 12902, vol)

@West: Ah! I did not know you could do that. In that case, your way is much better.

@West I didn’t see the volume explained in the manual, but volume shows when you edit the code. I tried different values just to see what would happen, but there didn’t seem to be any different between .5 and 100.

edit: The range seems to be from 0 to 1.

@dave1707 I’ve only really used the volume to play each sound I’m going to use at zero volume in setup to prevent a stutter the first time it is played in the main draw loop.

A wee test of

    sound(SOUND_PICKUP, 2160,ElapsedTime/100)

Indicates some volume control but it does appear to be valid over the range 0-0.5ish

Thanks everyone