在 Corona SDK composer 中,当我重新加载场景时,音频略微淡出

在我的 Corona SDK 项目中,我有一个使用 composer.newScene() 创建的 composer 场景称为“menu.lua”,是由 main.lua 文件调用的第一个场景。我只为这个场景加载了一个背景音轨,通过 audio.loadSound() 在本地变量中创建。当我加载另一个场景(假设是一个静态的“credit”场景,没有音乐,声音,动画,定时器等),然后回到菜单场景时,音频仍然播放,但音量较低。

音频在通道 2 上循环播放,我在 scene:show “did” 阶段使用 audio.play()。我在 scene:hide “will” 阶段使用 audio.fadeOut(),并在“did”阶段使用 audio.stop() 停止它,然后在 scene:destroy 中使用 audio.dispose() 处理它。

在“menu.lua”文件中

local composer=require("composer")
local scene=composer.newScene()
local theme --这是音频的变量

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

另一个场景(假设是“credits.lua”)是由带有“tap”事件的按钮调用的。在“credits.lua”中,我使用以下函数返回“menu.lua”场景(该函数带有“tap”事件附加到按钮)

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

我已经尝试在 scene:show “did” 阶段和 scene:create 中播放音频,但问题仍然存在。问题发生在所有静态场景中(总共有 3 个)。有什么想法吗?

点赞
用户7026995
用户7026995

你应该将

audio.fadeOut(theme,{500})

替换为

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

因为你使用了错误的语法。

参见 audio.fadeOut()

2019-01-31 09:55:49
用户1870706
用户1870706

请确保阅读文档中的“注意事项”部分:

当你淡出音量时,你正在改变通道的音量。这个值是持久化的,如果你想在以后再次使用该通道,你有责任重置通道的音量(参见 audio.setVolume())。

因为 fadeOut 改变了通道的音量,所以你要负责将通道音量设回原来的值。

2019-01-31 21:31:58