0
votes

In my Corona SDK project, I have a composer scene called "menu.lua" (created with composer.newScene()) that is the first scene, called by main.lua file. I have a background track only for this scene, loaded in scene:create() with audio.loadSound() in a local variable. When I load another scene (let's suppose it's a "credit" scene, static, with no music, sounds, animations, timers, etc.) and then come back to menu scene, audio is still played, but with a lower volume.

Audio is played in loop on channel 2, I use audio.play() in scene:show "did" phase. I use audio.fadeOut() in scene:hide "will" phase, and stop it with audio.stop() in "did" phase, then dispose it with audio.dispose() in scene:destroy.

In "menu.lua" file

local composer=require("composer")
local scene=composer.newScene()
local theme --this is the variable for audio

function scene:create(event)
  local sceneGroup=self.view
  theme=audio.loadSound("sfx/theme_short.mp3")
end

function scene:show(event)
  local sceneGroup=self.view
  if event.phase=="will"
    audio.play(theme,{loops=-1,channel=2})
  end
end

function scene:hide(event)
  local sceneGroup=self.view
  if event.phase=="will" then
    audio.fadeOut(theme,{500})
  end
  elseif event.phase=="did" then
    audio.stop(2)
  end
end

function scene:destroy(event)
  local sceneGroup=self.view
  audio.dispose(theme)
end

The other scene (let's suppose it's "credits.lua") is called by a button with a "tap" event attached. In "credits.lua" I use this function to go back to "menu.lua" scene (function is called with a "tap" event attached to a button)

local function goMenu()
  composer.removeScene("menu")
  composer.gotoScene("menu","slideUp",500)
  return true
end

I've already tried to play audio in scene:show "did" phase and in scene:create, but the problem persists. The problem happens with all the scenes, all static (3 in total). Any idea?

2

2 Answers

0
votes

You should replace

audio.fadeOut(theme,{500})

with

audio.fadeOut( { channel=2, time=500 } )

since you use wrong syntax.

See audio.fadeOut()

0
votes

Make sure you read the "Gotcha" section of the docs:

When you fade the volume, you are changing the volume of the channel. This value is persistent and it's your responsibility to reset the volume on the channel if you want to use the channel again later (see audio.setVolume()).

You're responsible for setting the channel volume back because fadeOut changes the volume of the channel.