源战役客户端
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.

485 lines
16 KiB

  1. LuaSoundManager = LuaSoundManager or BaseClass()
  2. local LuaSoundManager = LuaSoundManager
  3. local soundMgr = soundMgr
  4. local math_random = math.random
  5. LuaSoundManager.SOUND_TYPE =
  6. {
  7. SKILL = 1,
  8. UI = 2,
  9. ROLE = 3,
  10. FUNC = 4,
  11. NPC = 5,
  12. SCENE = 6,
  13. DROP = 7,
  14. GOD = 8,
  15. STORY = 9,
  16. GUIDE = 10,
  17. }
  18. LuaSoundManager.SOUND_PRE =
  19. {
  20. [1] = "sound_skill",
  21. [2] = "sound_ui",
  22. [3] = "sound_role",
  23. [4] = "sound_func",
  24. [5] = "sound_npc_",
  25. [6] = "sound_scene_",
  26. [7] = "sound_drop",
  27. [8] = "sound_god",
  28. [9] = "sound_story_",
  29. [10] = "sound_guide",
  30. }
  31. LuaSoundManager.SOUND_UI = {
  32. NONE = 9000,--不需要声音
  33. OPEN = 9001,--开界面
  34. CLOSE = 9002,--关界面
  35. CLICK = 9003,--点击
  36. SWITCH = 9003,--暂时没有区分音效和点击
  37. FAULT = 9004,--报错、无效操作、物品不足等
  38. AWARD = 9005,--打开宝箱
  39. DICE = 9006,--掷骰子
  40. SUCCESS = 9007,--升阶、分解、合成等操作成功(获得物品或其他数值奖励)
  41. UPGRADE = 9008, --完成任务
  42. LEVELUP = 9009, --升级
  43. EXPBULLETSHOT1 = 9010, -- 经验副本 霰射音效1
  44. EXPBULLETSHOT2 = 9011, -- 经验副本 霰射音效2
  45. EXPBULLETLASER = 9012, -- 经验副本 激光音效
  46. }
  47. function LuaSoundManager:__init()
  48. LuaSoundManager.Instance = self
  49. self.last_backgound_abName = nil
  50. self.current_scene_sound=""
  51. self.mute_request = {}
  52. self.sound_effect_time_list = {} --记录音效播放时间,cd控制用
  53. GlobalEventSystem:Bind(EventName.CHANGE_ACCOUNT, function()
  54. self:Reset()
  55. end)
  56. GlobalEventSystem:Bind(EventName.CHANGE_ROLE, function()
  57. self:Reset()
  58. end)
  59. self:InitVolumeCache()
  60. self:InitEvent()
  61. end
  62. function LuaSoundManager:InitEvent( )
  63. local function play_ui_effect( use_sound)
  64. self:PlayUIEffectSound(use_sound)
  65. end
  66. GlobalEventSystem:Bind(EventName.PLAY_UI_EFFECT_SOUND,play_ui_effect)
  67. end
  68. function LuaSoundManager:Reset()
  69. self.mute_request = {}
  70. end
  71. function LuaSoundManager:getInstance()
  72. if LuaSoundManager.Instance == nil then
  73. LuaSoundManager.New()
  74. end
  75. return LuaSoundManager.Instance
  76. end
  77. function LuaSoundManager:PlayLoginSound(sound_id, volume)
  78. if self.volume_cache_value and self.volume_cache_value <= 0 then
  79. return
  80. end
  81. local sound_type = LuaSoundManager.SOUND_TYPE.SCENE
  82. local ab_name = LuaSoundManager.SOUND_PRE[sound_type] .. tostring(sound_id)
  83. if self.last_backgound_abName and self.last_backgound_abName == ab_name then
  84. if volume then
  85. self:ChangeVolume(volume)
  86. end
  87. return
  88. end
  89. if self:StopBacksound() then
  90. self.last_backgound_abName = ab_name
  91. local back_ground_vol = volume or Config.ConfigSound.DefaultSceneVolume
  92. soundMgr:PlayBacksound(ab_name,tostring(sound_id),back_ground_vol)
  93. end
  94. end
  95. --播放场景音乐
  96. function LuaSoundManager:PlayBackground(scene_id, need_random)
  97. if self.volume_cache_value and self.volume_cache_value <= 0 then
  98. return
  99. end
  100. local scene_sound = Config.ConfigSound.DefaultScene
  101. local sound_cfg = Config.ConfigSound.Scene[scene_id]
  102. if sound_cfg then
  103. if #sound_cfg > 1 and need_random then
  104. local index = math_random(1, #sound_cfg)
  105. scene_sound = sound_cfg[index]
  106. else
  107. scene_sound = sound_cfg[1]
  108. end
  109. elseif ConfigItemMgr.Instance:GetSceneItem(scene_id) then
  110. local scene_type = ConfigItemMgr.Instance:GetSceneItem(scene_id).type
  111. if Config.ConfigSound.DefaultSceneType[scene_type] then
  112. scene_sound = Config.ConfigSound.DefaultSceneType[scene_type]
  113. end
  114. end
  115. self.current_scene_sound = scene_sound
  116. local back_ground_vol =nil
  117. if Config.ConfigSound.DefaultSceneTypeVolume[self.current_scene_sound] then
  118. back_ground_vol = Config.ConfigSound.DefaultSceneTypeVolume[self.current_scene_sound]
  119. else
  120. back_ground_vol = Config.ConfigSound.DefaultSceneVolume
  121. end
  122. local sound_type = LuaSoundManager.SOUND_TYPE.SCENE
  123. local ab_name = LuaSoundManager.SOUND_PRE[sound_type] .. tostring(scene_sound)
  124. local setting_music_vol = self.cookieSetting.sound * 0.01
  125. self.cur_backgound_abName = ab_name
  126. self.cur_backgound_vol = back_ground_vol and back_ground_vol * setting_music_vol or setting_music_vol
  127. self.cur_scene_sound = scene_sound
  128. --首次的话直接播放
  129. if not self.last_backgound_abName then
  130. soundMgr:PlayBacksound(self.cur_backgound_abName,tostring(self.cur_scene_sound),back_ground_vol)
  131. return
  132. end
  133. --渐变过渡
  134. if self.last_backgound_abName ~= ab_name then
  135. local cur_vol = soundMgr.volume
  136. local function fadeOut()
  137. cur_vol = cur_vol - 0.02
  138. if cur_vol < 0.1 then
  139. cur_vol = 0.1
  140. end
  141. -- print("fadeOut ",cur_vol)
  142. self:ChangeVolume(cur_vol)
  143. if cur_vol <= 0.1 then
  144. self.last_backgound_vol = 0
  145. -- print("播放音乐 ",self.cur_backgound_abName,self.cur_scene_sound,self.cur_backgound_vol)
  146. self:StopBacksound()
  147. soundMgr:PlayBacksound(self.cur_backgound_abName,tostring(self.cur_scene_sound),back_ground_vol)
  148. local function fadeIn()
  149. self.last_backgound_vol = self.last_backgound_vol + 0.01
  150. local targetVol = self.cur_backgound_vol
  151. if self.last_backgound_vol >= targetVol then
  152. self.last_backgound_vol = targetVol
  153. end
  154. self:ChangeVolume(self.last_backgound_vol)
  155. -- print("fadeIn ",self.last_backgound_vol)
  156. if self.last_backgound_vol >= targetVol then
  157. GlobalTimerQuest:CancelQuest(self.fade_in_id)
  158. self.fade_in_id = nil
  159. self.last_backgound_abName = self.cur_backgound_abName
  160. self.last_backgound_vol = targetVol
  161. end
  162. end
  163. local function delay()
  164. if not self.fade_in_id then
  165. self.fade_in_id = GlobalTimerQuest:AddPeriodQuest(fadeIn,0.02,-1)
  166. end
  167. end
  168. setTimeout(delay,0.5) --假装等待加载
  169. GlobalTimerQuest:CancelQuest(self.fade_out_id)
  170. self.fade_out_id = nil
  171. end
  172. end
  173. if not self.fade_out_id then
  174. self.fade_out_id = GlobalTimerQuest:AddPeriodQuest(fadeOut,0.02,-1)
  175. end
  176. end
  177. end
  178. function LuaSoundManager:PlayBackgroundSound(sound_name, sound_type, force)
  179. if sound_name == self.current_scene_sound and not force then
  180. return
  181. end
  182. if self.volume_effect_cache_value and self.volume_effect_cache_value <= 0 then
  183. return
  184. end
  185. self.current_scene_sound = sound_name
  186. local back_ground_vol =nil
  187. if Config.ConfigSound.DefaultSceneTypeVolume[self.current_scene_sound] then
  188. back_ground_vol = Config.ConfigSound.DefaultSceneTypeVolume[self.current_scene_sound]
  189. else
  190. back_ground_vol = Config.ConfigSound.DefaultSceneVolume
  191. end
  192. local ab_name = LuaSoundManager.SOUND_PRE[sound_type]..tostring(sound_name)
  193. local setting_music_vol = self.cookieSetting.sound * 0.01
  194. self.cur_backgound_abName = ab_name
  195. self.cur_backgound_vol = back_ground_vol and back_ground_vol * setting_music_vol or setting_music_vol
  196. --首次的话直接播放
  197. if not self.last_backgound_abName then
  198. soundMgr:PlayBacksound(self.cur_backgound_abName,tostring(sound_name),back_ground_vol)
  199. return
  200. end
  201. --渐变过渡
  202. if self.last_backgound_abName ~= ab_name or force then
  203. local cur_vol = soundMgr.volume
  204. local function fadeOut()
  205. cur_vol = cur_vol - 0.02
  206. if cur_vol < 0.1 then
  207. cur_vol = 0.1
  208. end
  209. self:ChangeVolume(cur_vol)
  210. if cur_vol <= 0.1 then
  211. self.last_backgound_vol = 0
  212. self:StopBacksound()
  213. soundMgr:PlayBacksound(self.cur_backgound_abName,sound_name,back_ground_vol)
  214. local function fadeIn()
  215. self.last_backgound_vol = self.last_backgound_vol + 0.01
  216. local targetVol = self.cur_backgound_vol
  217. if self.last_backgound_vol >= targetVol then
  218. self.last_backgound_vol = targetVol
  219. end
  220. self:ChangeVolume(self.last_backgound_vol)
  221. if self.last_backgound_vol >= targetVol then
  222. GlobalTimerQuest:CancelQuest(self.fade_in_id)
  223. self.fade_in_id = nil
  224. self.last_backgound_abName = self.cur_backgound_abName
  225. self.last_backgound_vol = targetVol
  226. end
  227. end
  228. local function delay()
  229. if not self.fade_in_id then
  230. self.fade_in_id = GlobalTimerQuest:AddPeriodQuest(fadeIn,0.02,-1)
  231. end
  232. end
  233. setTimeout(delay,0.5) --假装等待加载
  234. GlobalTimerQuest:CancelQuest(self.fade_out_id)
  235. self.fade_out_id = nil
  236. end
  237. end
  238. if not self.fade_out_id then
  239. self.fade_out_id = GlobalTimerQuest:AddPeriodQuest(fadeOut,0.02,-1)
  240. end
  241. end
  242. end
  243. --继续播放背景音乐
  244. function LuaSoundManager:ContinueBacksound()
  245. soundMgr:ContinueBacksound()
  246. end
  247. --暂停背景音乐
  248. function LuaSoundManager:PauseBacksound()
  249. soundMgr:PauseBacksound()
  250. end
  251. --停止背景音乐
  252. function LuaSoundManager:StopBacksound()
  253. if self.last_backgound_abName then
  254. soundMgr:StopBacksound()
  255. resMgr:UnloadAssetBundle(self.last_backgound_abName, true, 1)
  256. end
  257. return true
  258. end
  259. --播放指定音效
  260. function LuaSoundManager:PlayEffect(ref_tar, path, is_loop, sound_type, vol_modulus, speed)
  261. if not path then return end
  262. --首次登陆还未进入游戏,协议返回的成功失败等消息不要有声音
  263. if RoleController and RoleController.Instance then
  264. if not RoleController.Instance.is_load_and_enter_finished then
  265. if path == "award" or path == "success" or path == "upgrade" or path == "fault" then
  266. return
  267. end
  268. end
  269. end
  270. if self.volume_effect_cache_value and self.volume_effect_cache_value < 0.01 then
  271. return path
  272. end
  273. local res_name = path
  274. local ab_name = LuaSoundManager.SOUND_PRE[sound_type]
  275. if sound_type == LuaSoundManager.SOUND_TYPE.NPC or sound_type == LuaSoundManager.SOUND_TYPE.STORY then --NPC音效变成了单独打包
  276. ab_name = LuaSoundManager.SOUND_PRE[sound_type]..tostring(res_name)
  277. end
  278. if not vol_modulus then
  279. if Config.ConfigSound.SoundEffect[sound_type] then
  280. if Config.ConfigSound.SoundEffect[sound_type][path] then
  281. vol_modulus = Config.ConfigSound.SoundEffect[sound_type][path]
  282. else
  283. vol_modulus = Config.ConfigSound.DefaultSoundEffectTypeVolume[sound_type]
  284. end
  285. else
  286. vol_modulus = Config.ConfigSound.DefaultSoundEffectVolume
  287. end
  288. end
  289. self.sound_effect_time_list[path] = Time.time
  290. local effect_id = lua_resM:loadSound(ref_tar, ab_name, tostring(res_name), is_loop,vol_modulus, speed)
  291. return effect_id or path
  292. end
  293. --停止指定音效
  294. function LuaSoundManager:StopEffect(ref_tar, sound_type, effect_id)
  295. local ab_name = LuaSoundManager.SOUND_PRE[sound_type]
  296. lua_resM:stopSound(ref_tar, ab_name, effect_id)
  297. end
  298. --加载语音
  299. function LuaSoundManager:LoadVoice(path, callback)
  300. soundMgr:LoadVoice(path, callback)
  301. end
  302. --初始化音量值
  303. function LuaSoundManager:InitVolumeCache( )
  304. self.volume_cache_value = soundMgr.volume
  305. self.volume_effect_cache_value = soundMgr.volumeEffect
  306. self.last_volume_cache_value = self.volume_cache_value
  307. end
  308. --改变声音音量大小
  309. function LuaSoundManager:ChangeVolume(value)
  310. soundMgr.volume = value
  311. self.volume_cache_value = value
  312. if self.last_volume_cache_value and self.last_volume_cache_value <= 0 and value and value > 0 then
  313. --播放场景背景音乐
  314. self.last_backgound_abName = false
  315. local cur_scene = SceneManager:getInstance():GetSceneId()
  316. self:PlayBackground(cur_scene)
  317. end
  318. self.last_volume_cache_value = value
  319. end
  320. --改变音效音量大小
  321. function LuaSoundManager:ChangeVolumeEffect(value)
  322. self.volume_effect_cache_value = value
  323. soundMgr.volumeEffect = value
  324. end
  325. --是否音效静音
  326. function LuaSoundManager:IsEffectMute( )
  327. if self.volume_effect_cache_value and self.volume_effect_cache_value <= 0.1 then
  328. return true
  329. else
  330. return false
  331. end
  332. end
  333. --开关背景音乐
  334. function LuaSoundManager:SwitchMusicState(open)
  335. soundMgr.musicSwitch = open
  336. end
  337. --开关音效
  338. function LuaSoundManager:SwitchEffectState(open)
  339. soundMgr.effectSwitch = open
  340. end
  341. --添加静音请求
  342. function LuaSoundManager:AddMuteQuest(obj)
  343. if not soundMgr.musicSwitch then soundMgr.musicSwitch = false end
  344. if not soundMgr.effectSwitch then soundMgr.effectSwitch = false end
  345. self.mute_request[obj] = true
  346. if RuntimePlatform and SystemRuntimePlatform.IsIphone() then
  347. self.ios_last_scene_sound = self.current_scene_sound
  348. self:PauseBacksound()
  349. end
  350. end
  351. --移除静音请求
  352. function LuaSoundManager:CancelMuteQuest(obj)
  353. self.mute_request[obj] = nil
  354. if TableSize(self.mute_request) == 0 then
  355. if soundMgr.musicSwitch then soundMgr.musicSwitch = true end
  356. if soundMgr.effectSwitch then soundMgr.effectSwitch = true end
  357. end
  358. if self.ios_last_scene_sound then
  359. self:ContinueBacksound()
  360. self.ios_last_scene_sound = nil
  361. end
  362. end
  363. --恢复所有声音
  364. function LuaSoundManager:StartAllVoice()
  365. -- soundMgr.volume = 1
  366. self:LoadSetting()
  367. end
  368. function LuaSoundManager:PlayingSoundEffect(name, cd)
  369. -- return soundMgr:PlayingSoundEffect(name) --有gc的开销,先在lua层缓存时间做CD
  370. local cd_time = cd or 0.2
  371. if self.sound_effect_time_list and self.sound_effect_time_list[name] then
  372. return self.sound_effect_time_list[name] + cd_time > Time.time
  373. end
  374. end
  375. function LuaSoundManager:LoadSetting()
  376. -- local cookieSetting = CookieWrapper.Instance:GetCookie(CookieLevelType.Common,CookieKey.SYSTEM_SETTING)
  377. -- if cookieSetting then
  378. -- soundMgr.musicSwitch = cookieSetting.musicSound
  379. -- soundMgr.effectSwitch = cookieSetting.effectSound
  380. -- end
  381. self.cookieSetting = CookieWrapper.Instance:GetCookie(CookieLevelType.Common,CookieKey.SOUND_SETTING)
  382. self.cookieSetting = self.cookieSetting or {sound = Config.ConfigSound.TotalVloume, effectSound = Config.ConfigSound.TotalSoundEffect}
  383. if self.cookieSetting then
  384. self:ChangeVolume(self.cookieSetting.sound / 100)
  385. self:ChangeVolumeEffect(self.cookieSetting.effectSound / 100)
  386. end
  387. end
  388. function LuaSoundManager:CreateAudioClip(voice_data, data_ver, callback)
  389. if data_ver <= 29 then
  390. return soundMgr:CreateAudioClip(voice_data)
  391. elseif data_ver <= 32 then
  392. return soundMgr:CreateAudioClipFromCompress(voice_data)
  393. else
  394. if Application.isMobilePlatform then
  395. return soundMgr:CreateAudioClip(voice_data, AppConst.CachedVoicePath.."/tmp.mp3", callback)
  396. else
  397. return soundMgr:CreateAudioClip(voice_data, AppConst.CachedVoicePath.."/tmp.wav", callback)
  398. end
  399. end
  400. end
  401. function LuaSoundManager:PlayUIEffectSound(use_sound)
  402. if not use_sound then
  403. return
  404. end
  405. if lua_soundM then
  406. if use_sound == LuaSoundManager.SOUND_UI.OPEN and not lua_soundM:PlayingSoundEffect("window_open", 0.7) then
  407. self:PlayEffect(self, "window_open",false,LuaSoundManager.SOUND_TYPE.UI)
  408. elseif use_sound == LuaSoundManager.SOUND_UI.CLOSE and not lua_soundM:PlayingSoundEffect("window_close", 0.2) then
  409. self:PlayEffect(self, "window_close",false,LuaSoundManager.SOUND_TYPE.UI)
  410. elseif use_sound == LuaSoundManager.SOUND_UI.SWITCH or use_sound == LuaSoundManager.SOUND_UI.CLICK then
  411. self:PlayEffect(self, "click",false,LuaSoundManager.SOUND_TYPE.UI)
  412. elseif use_sound == LuaSoundManager.SOUND_UI.FAULT and not lua_soundM:PlayingSoundEffect("fault", 1) then
  413. self:PlayEffect(self, "fault",false,LuaSoundManager.SOUND_TYPE.UI)
  414. elseif use_sound == LuaSoundManager.SOUND_UI.AWARD and not lua_soundM:PlayingSoundEffect("award", 1.3) then
  415. self:PlayEffect(self, "award",false,LuaSoundManager.SOUND_TYPE.UI)
  416. elseif use_sound == LuaSoundManager.SOUND_UI.DICE and not lua_soundM:PlayingSoundEffect("dice", 2.6) then
  417. self:PlayEffect(self, "dice",false,LuaSoundManager.SOUND_TYPE.UI)
  418. elseif use_sound == LuaSoundManager.SOUND_UI.SUCCESS and not lua_soundM:PlayingSoundEffect("success", 0.2) then
  419. self:PlayEffect(self, "success",false,LuaSoundManager.SOUND_TYPE.UI)
  420. elseif use_sound == LuaSoundManager.SOUND_UI.UPGRADE and not lua_soundM:PlayingSoundEffect("upgrade", 0.1) then
  421. self:PlayEffect(self, "upgrade",false,LuaSoundManager.SOUND_TYPE.ROLE, 1)
  422. elseif use_sound == LuaSoundManager.SOUND_UI.LEVELUP and not lua_soundM:PlayingSoundEffect("levelup", 0.2) then
  423. self:PlayEffect(self, "levelup",false,LuaSoundManager.SOUND_TYPE.ROLE, 1, 1, true)
  424. elseif use_sound == LuaSoundManager.SOUND_UI.EXPBULLETSHOT1 and not lua_soundM:PlayingSoundEffect("exp_bullet_shot1", 0.6) then
  425. self:PlayEffect(self, "exp_bullet_shot1",false,LuaSoundManager.SOUND_TYPE.UI, 1, 1, true)
  426. elseif use_sound == LuaSoundManager.SOUND_UI.EXPBULLETSHOT2 and not lua_soundM:PlayingSoundEffect("exp_bullet_shot2", 0.6) then
  427. self:PlayEffect(self, "exp_bullet_shot2",false,LuaSoundManager.SOUND_TYPE.UI, 1, 1, true)
  428. elseif use_sound == LuaSoundManager.SOUND_UI.EXPBULLETLASER and not lua_soundM:PlayingSoundEffect("exp_bullet_laser", 0.6) then
  429. self:PlayEffect(self, "exp_bullet_laser",false,LuaSoundManager.SOUND_TYPE.UI, 1, 1, true)
  430. end
  431. end
  432. end
  433. function LuaSoundManager:PlayDungeonResultSound( is_win )
  434. local career = RoleManager.Instance.mainRoleInfo.career or 1
  435. local prefix = is_win and "victor_" or "fail_"
  436. local suffix = is_win and math_random(1,2) or 1
  437. local res_name = prefix .. career*1000 .. "_" .. suffix
  438. self:PlayEffect(self, res_name, false, LuaSoundManager.SOUND_TYPE.ROLE, 1)
  439. end