You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.7 KiB

  1. return function()
  2. -- 当前正在播放的背景音乐
  3. local currentBGPlayer;
  4. local currentBGMusic;
  5. local currentBGDelay;
  6. local currentPlayConfig;
  7. local bindSoundIndex;
  8. -- 强制停止当前背景音乐
  9. local function _stopCustomMusic()
  10. if currentBGPlayer then
  11. -- 停止计时
  12. currentBGPlayer:cancelTimer();
  13. -- 淡出
  14. if not tolua.isnull(currentBGMusic) then
  15. local timerId;
  16. local fadeOutTime = currentBGPlayer:getConfig().FadeOut;
  17. local sound = currentBGMusic;
  18. if fadeOutTime <= 0 then
  19. if not tolua.isnull(sound) then
  20. --print("停止音乐" , debug.traceback());
  21. cc.AudioController:getInstance():stopSound(sound);
  22. end
  23. else
  24. local function interpolate(from , to , t)
  25. -- SINE_IN_OUT
  26. t = -0.5 * (math.cos(math.pi * t) - 1.0);
  27. return from + (to - from) * t;
  28. end
  29. local fromGain = sound:getGain();
  30. local offset = 0;
  31. local function update(dt)
  32. offset = offset + dt;
  33. if offset >= fadeOutTime then
  34. cc.Director:getInstance():getScheduler():unscheduleScriptEntry(timerId);
  35. if not tolua.isnull(sound) then
  36. --print("停止音乐" , debug.traceback());
  37. cc.AudioController:getInstance():stopSound(sound);
  38. end
  39. end
  40. if not tolua.isnull(sound) then
  41. local gain = interpolate(fromGain , 0 , offset / fadeOutTime);
  42. sound:setGain(gain);
  43. else
  44. cc.Director:getInstance():getScheduler():unscheduleScriptEntry(timerId);
  45. end
  46. end
  47. -- 一段时间检测一下
  48. timerId = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update, 0, false);
  49. end
  50. end
  51. -- 结束
  52. currentBGPlayer = nil;
  53. -- 清空下配置
  54. --currentPlayConfig = nil
  55. end
  56. currentBGMusic = nil;
  57. end
  58. -- 播放背景音乐
  59. --[[
  60. -- 保存所有配置
  61. playConfig =
  62. {
  63. -- 唯一的名字,如果正在播的是这个名字,就忽略
  64. Name = "login";
  65. -- 需要播放的声音列表,会从中随机一首
  66. Files = {"res/sound/BW/BW1.ogg" , "res/sound/BW/BW2.ogg"};
  67. -- 播放几率,[0-100]
  68. Rate = 100;
  69. -- 每批的间隔
  70. IntervalMin = 0;
  71. IntervalMax = 0;
  72. -- 一批播放多少次
  73. PlayCountMin = 1;
  74. PlayCountMax = 1;
  75. -- 循环多少批,0表示无限循环
  76. LoopCount = 1;
  77. -- 延迟几秒才开始播放
  78. DelayTime = 3;
  79. -- 声音停止时的淡出时间(秒)
  80. FadeOut = 3;
  81. }
  82. --]]
  83. -- 播放音乐
  84. local function _playCustomMusic(playConfig)
  85. -- 先停止音乐
  86. _stopCustomMusic();
  87. -- 停止音乐
  88. local function stop()
  89. if not tolua.isnull(currentBGMusic) then
  90. --print("停止音乐" , debug.traceback());
  91. cc.AudioController:getInstance():stopSound(currentBGMusic);
  92. end
  93. currentBGMusic = nil;
  94. end
  95. -- 播放音乐
  96. local function play()
  97. stop();
  98. local filePath = playConfig.Files[math.random(1 , #playConfig.Files)];
  99. --print("播放音乐" , debug.traceback());
  100. cc.AudioController:getInstance():setPlaySound(true)
  101. currentBGMusic = cc.AudioController:getInstance():playSound(filePath , true);
  102. cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound)
  103. -- currentBGMusic 如果为空直接返回
  104. if not currentBGMusic then
  105. return
  106. end
  107. local musicVolume = app.systemSetting.info.musicVolume
  108. currentBGMusic:setGain(musicVolume)
  109. currentBGMusic:play();
  110. local function onEvent(state)
  111. -- 结束通知
  112. if state == 3 and currentBGMusic then
  113. currentBGMusic = nil;
  114. currentBGPlayer:onPlayEnd();
  115. end
  116. end
  117. currentBGMusic:addListener(onEvent);
  118. end
  119. currentBGPlayer = cc.RandomPlayer:new();
  120. currentBGPlayer:setPlayFunc(play);
  121. currentBGPlayer:setStopFunc(stop);
  122. -- 配置(播放音乐必须的配置)
  123. currentBGPlayer:setConfig(playConfig);
  124. -- 开始播放
  125. currentBGPlayer:start(app.mainScene);
  126. end
  127. -- 停止背景音乐
  128. local function stopBGMusic()
  129. -- 取消延迟
  130. if currentBGDelay then
  131. currentBGDelay();
  132. currentBGDelay = nil;
  133. end
  134. if bindSoundIndex then
  135. app.mainScene:unbind(app.systemSetting.info, "music", bindSoundIndex);
  136. bindSoundIndex = nil;
  137. end
  138. _stopCustomMusic();
  139. end
  140. -- 播放背景音乐
  141. local function playBGMusic(playConfig)
  142. -- 如果当前正在播放,且声音相同,就忽略
  143. if currentBGPlayer and currentPlayConfig and currentPlayConfig.Name == playConfig.Name then
  144. return;
  145. end
  146. -- 先停止音乐
  147. stopBGMusic()
  148. local isTest = false;
  149. if not isTest then
  150. playConfig.IntervalMax = math.max(playConfig.IntervalMin , playConfig.IntervalMax);
  151. playConfig.PlayCountMax = math.max(playConfig.PlayCountMin , playConfig.PlayCountMax);
  152. -- LUA层的配置
  153. currentPlayConfig = playConfig
  154. -- 开始播放音乐
  155. local function onMusicSettingChanged(key , value)
  156. if not value then
  157. -- 停止当前音乐
  158. _stopCustomMusic()
  159. else
  160. -- 如果已经开放播放了则不需要播放了
  161. if not currentBGPlayer and currentPlayConfig then
  162. _playCustomMusic(currentPlayConfig)
  163. end
  164. end
  165. end
  166. local function onMusicVolumeChanged(key, value)
  167. local musicVolume = app.systemSetting.info.musicVolume
  168. if currentBGMusic then
  169. currentBGMusic:setGain(musicVolume)
  170. end
  171. end
  172. -- 开始播放音乐
  173. local function start()
  174. currentBGDelay = nil;
  175. bindSoundIndex = app.mainScene:bind(app.systemSetting.info, "music" , onMusicSettingChanged);
  176. app.mainScene:bind(app.systemSetting.info, "musicVolume", onMusicVolumeChanged)
  177. end
  178. -- 没有延迟立马开始
  179. if playConfig.DelayTime == nil or playConfig.DelayTime <= 0 then
  180. start();
  181. else
  182. currentBGDelay = runDelay(playConfig.DelayTime, start);
  183. end
  184. end
  185. end
  186. return { playBGMusic = playBGMusic , stopBGMusic = stopBGMusic};
  187. end;