return function() -- 当前正在播放的背景音乐 local currentBGPlayer; local currentBGMusic; local currentBGDelay; local currentPlayConfig; local bindSoundIndex; -- 强制停止当前背景音乐 local function _stopCustomMusic() if currentBGPlayer then -- 停止计时 currentBGPlayer:cancelTimer(); -- 淡出 if not tolua.isnull(currentBGMusic) then local timerId; local fadeOutTime = currentBGPlayer:getConfig().FadeOut; local sound = currentBGMusic; if fadeOutTime <= 0 then if not tolua.isnull(sound) then --print("停止音乐" , debug.traceback()); cc.AudioController:getInstance():stopSound(sound); end else local function interpolate(from , to , t) -- SINE_IN_OUT t = -0.5 * (math.cos(math.pi * t) - 1.0); return from + (to - from) * t; end local fromGain = sound:getGain(); local offset = 0; local function update(dt) offset = offset + dt; if offset >= fadeOutTime then cc.Director:getInstance():getScheduler():unscheduleScriptEntry(timerId); if not tolua.isnull(sound) then --print("停止音乐" , debug.traceback()); cc.AudioController:getInstance():stopSound(sound); end end if not tolua.isnull(sound) then local gain = interpolate(fromGain , 0 , offset / fadeOutTime); sound:setGain(gain); else cc.Director:getInstance():getScheduler():unscheduleScriptEntry(timerId); end end -- 一段时间检测一下 timerId = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update, 0, false); end end -- 结束 currentBGPlayer = nil; -- 清空下配置 --currentPlayConfig = nil end currentBGMusic = nil; end -- 播放背景音乐 --[[ -- 保存所有配置 playConfig = { -- 唯一的名字,如果正在播的是这个名字,就忽略 Name = "login"; -- 需要播放的声音列表,会从中随机一首 Files = {"res/sound/BW/BW1.ogg" , "res/sound/BW/BW2.ogg"}; -- 播放几率,[0-100] Rate = 100; -- 每批的间隔 IntervalMin = 0; IntervalMax = 0; -- 一批播放多少次 PlayCountMin = 1; PlayCountMax = 1; -- 循环多少批,0表示无限循环 LoopCount = 1; -- 延迟几秒才开始播放 DelayTime = 3; -- 声音停止时的淡出时间(秒) FadeOut = 3; } --]] -- 播放音乐 local function _playCustomMusic(playConfig) -- 先停止音乐 _stopCustomMusic(); -- 停止音乐 local function stop() if not tolua.isnull(currentBGMusic) then --print("停止音乐" , debug.traceback()); cc.AudioController:getInstance():stopSound(currentBGMusic); end currentBGMusic = nil; end -- 播放音乐 local function play() stop(); local filePath = playConfig.Files[math.random(1 , #playConfig.Files)]; --print("播放音乐" , debug.traceback()); cc.AudioController:getInstance():setPlaySound(true) currentBGMusic = cc.AudioController:getInstance():playSound(filePath , true); cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound) -- currentBGMusic 如果为空直接返回 if not currentBGMusic then return end local musicVolume = app.systemSetting.info.musicVolume currentBGMusic:setGain(musicVolume) currentBGMusic:play(); local function onEvent(state) -- 结束通知 if state == 3 and currentBGMusic then currentBGMusic = nil; currentBGPlayer:onPlayEnd(); end end currentBGMusic:addListener(onEvent); end currentBGPlayer = cc.RandomPlayer:new(); currentBGPlayer:setPlayFunc(play); currentBGPlayer:setStopFunc(stop); -- 配置(播放音乐必须的配置) currentBGPlayer:setConfig(playConfig); -- 开始播放 currentBGPlayer:start(app.mainScene); end -- 停止背景音乐 local function stopBGMusic() -- 取消延迟 if currentBGDelay then currentBGDelay(); currentBGDelay = nil; end if bindSoundIndex then app.mainScene:unbind(app.systemSetting.info, "music", bindSoundIndex); bindSoundIndex = nil; end _stopCustomMusic(); end -- 播放背景音乐 local function playBGMusic(playConfig) -- 如果当前正在播放,且声音相同,就忽略 if currentBGPlayer and currentPlayConfig and currentPlayConfig.Name == playConfig.Name then return; end -- 先停止音乐 stopBGMusic() local isTest = false; if not isTest then playConfig.IntervalMax = math.max(playConfig.IntervalMin , playConfig.IntervalMax); playConfig.PlayCountMax = math.max(playConfig.PlayCountMin , playConfig.PlayCountMax); -- LUA层的配置 currentPlayConfig = playConfig -- 开始播放音乐 local function onMusicSettingChanged(key , value) if not value then -- 停止当前音乐 _stopCustomMusic() else -- 如果已经开放播放了则不需要播放了 if not currentBGPlayer and currentPlayConfig then _playCustomMusic(currentPlayConfig) end end end local function onMusicVolumeChanged(key, value) local musicVolume = app.systemSetting.info.musicVolume if currentBGMusic then currentBGMusic:setGain(musicVolume) end end -- 开始播放音乐 local function start() currentBGDelay = nil; bindSoundIndex = app.mainScene:bind(app.systemSetting.info, "music" , onMusicSettingChanged); app.mainScene:bind(app.systemSetting.info, "musicVolume", onMusicVolumeChanged) end -- 没有延迟立马开始 if playConfig.DelayTime == nil or playConfig.DelayTime <= 0 then start(); else currentBGDelay = runDelay(playConfig.DelayTime, start); end end end return { playBGMusic = playBGMusic , stopBGMusic = stopBGMusic}; end;