Corona SDK Pro Tip of the Day #17
Sounds by name

Cache loaded sounds and play them by names.

The idea is the same as with colors or reference points. Instead of passing audio handles directly, just use string names!

This is my app.lua again, but I modify audio module instead, just makes more sense I guess, but if you don't like it, you can change it to _M.playSFX().

local _M = {}

_M.sound_on = true

local sounds = {}  
sounds['play'] = 'sounds/play.wav'  
sounds['pop'] = 'sounds/pop.wav'  
sounds['button'] = 'sounds/button.wav'

local loadedSounds = {}  
local function loadSound(snd)  
    if not loadedSounds[snd] then
        loadedSounds[snd] = audio.loadSound(sounds[snd])
    end
    return loadedSounds[snd]
end

function audio.playSFX(snd, params)  
    if not _M.sound_on then return end
    local channel = (type(snd) == 'string') and audio.play(loadSound(snd), params) or audio.play(snd, params)
    audio.setVolume(1, {channel = channel})
    return channel
end

return _M  

loadedSounds is our cache of sound handles.

In audio.playSFX() we check if the sound is turned on by the user (notice _M.sound_on = true) and see if the snd value is a string. If so - get the sound handle from cache (and put it there if there is none). If it's not a string - call usual audio.play instead.

sounds table can be stored in a separate file if you wish.

Indie Game Developer