how to set audio as a default

hey guys…

in my game options…i have an option of audio…if i set sudio to ‘on’ then how can i make it play for all the scenes?? means it should not switch off when we switch scenes…it should continue playing…and suppose i have game.lua in which an audio plays when a player hits something…then how to switch off that particular audio from my options.lua ??

Thanks

Hi,

It’s hard to give a direct answer here without knowing your setup, but it to my knowledge, Corona does not do anything to your music or audio when you change scenes. Somewhere in your code you likely have something that changes the audio or turns it off.

To switch off your audio, you can use the audio API, such as audio.stop (https://docs.coronalabs.com/api/library/audio/stop.html). You’ll need a way to check your options.lua, for example:

local options = require("options"); -- Done at initialization local myAudioHandle = audio.loadSound("mySound.wav"); local myChannel = nil; local function onTap(event) if event.phase == "began" then if options.isAudioEnabled == true then -- Notice that it checks your setting before playing myChannel = audio.play(myAudioHandle); end elseif event.phase == "ended" then audio.stop(myChannel); -- You can stop an active sound with audio.stop() end end local rect = display.newRect(display.contentCenterX, display.contentCenterY, 100, 100); rect:addEventListener("tap", onTap);

Hopefully that helps

Albert

Hi,

It’s hard to give a direct answer here without knowing your setup, but it to my knowledge, Corona does not do anything to your music or audio when you change scenes. Somewhere in your code you likely have something that changes the audio or turns it off.

To switch off your audio, you can use the audio API, such as audio.stop (https://docs.coronalabs.com/api/library/audio/stop.html). You’ll need a way to check your options.lua, for example:

local options = require("options"); -- Done at initialization local myAudioHandle = audio.loadSound("mySound.wav"); local myChannel = nil; local function onTap(event) if event.phase == "began" then if options.isAudioEnabled == true then -- Notice that it checks your setting before playing myChannel = audio.play(myAudioHandle); end elseif event.phase == "ended" then audio.stop(myChannel); -- You can stop an active sound with audio.stop() end end local rect = display.newRect(display.contentCenterX, display.contentCenterY, 100, 100); rect:addEventListener("tap", onTap);

Hopefully that helps

Albert