local Sound_Controller = {} local private = {} local EventSystem = require('Event/EventSystem') local LuaFuncEntry = require("Main/LuaFuncEntry") local uiListener = true local isStopFootSound = false local soundNameToId ={} local SoundUtil=SoundUtil local levelSoundObjList = {} local LocalizationCfg = require "Localization/LocalizationCfg" local resumeMovies = {"Plot0201_01.mp4", "Plot0204_01.mp4", "Plot100_07_01.mp4", "Plot100_07_03.mp4", "Plot10307009.mp4", "Plot10503005.mp4"} local applicationQuitting = false function Sound_Controller.OnStartup() private.SetupEntryFunc(true) EventSystem.Regist(EventID.PlayGameSound, Sound_Controller.PlayGameSound) EventSystem.Regist(EventID.StopGameSound, Sound_Controller.StopGameSound) EventSystem.Regist(EventID.ApplicationQuit, private.OnApplicationQuit) EventSystem.Regist(EventID.UI_Delay_FocusOnTownHome, private.OnFocusOnTownHome) end function private.SetupEntryFunc(isRegist) LuaFuncEntry.SetupMainEntryFunc(isRegist, LuaFuncEntry.FuncType.PlayDialogue, private.DelayPlayDialogue, LuaFuncEntry.FuncType.SkipTimeline, private.ClearDialogueQueue, LuaFuncEntry.FuncType.SetVolume, private.SetVolume, LuaFuncEntry.FuncType.ResetVolume, private.ResetVolume ) end function private.SetVolume() MDL.GuildWar.SetVolume() CTL.SyncTeamPVP.SetVolume() end function private.ResetVolume() MDL.GuildWar.ResetVolume() CTL.SyncTeamPVP.ResetVolume() end function Sound_Controller.OnShutdown() SoundUtil.StopAll() if not applicationQuitting then SoundUtil.ResetSoundEngine() end soundNameToId = {} private.SetupEntryFunc(false) end function Sound_Controller.Init() private.SetAudioLanguage() SoundUtil.AddDontUnloadBank("UI_common"); SoundUtil.AddDontUnloadBank("MUSIC"); SoundUtil.AddDontUnloadBank("new_guidedata"); Sound_Controller.InitSoundVolume() Sound_Controller.PlayGameSound("MUSIC") Sound_Controller.killGapMaxCount = -1 -- 记录击杀数量达到最高值的未播放次数(每隔2次才能播放一次) end function Sound_Controller.PlayGameSound(name,isSave) local soundId=UTL.String.GetIdByString(name) local id = SoundUtil.PlaySoundByEvent(soundId) if isSave then soundNameToId[name] = id end return id end function Sound_Controller.StopGameSound(name) local id = soundNameToId[name] if id then --logging.log(LTAG.XuQian,"StopGameSound ",name,id) Sound_Controller.StopById(id) end end function Sound_Controller.PlayObjSound(name,Obj) local soundId=UTL.String.GetIdByString(name) return SoundUtil.PlaySoundByEvent(soundId,Obj) end function Sound_Controller.CheckAndPlay(name) if SoundUtil.ContainsEvent(name) then Sound_Controller.PlayGameSound(name) end end function Sound_Controller.SetSwitch(switchGroup, state,gameObject) SoundUtil.SetSwitch(switchGroup, state,gameObject) end function Sound_Controller.SetRTPCValue(name,value) local id=UTL.String.GetIdByString(name) SoundUtil.SetRTPCValue(id,value) end function Sound_Controller.SetRTPCValueByObj(name,value,obj) local id=UTL.String.GetIdByString(name) SoundUtil.SetRTPCValue(id,value,obj) end function Sound_Controller.UpdateBloodValue(blood) Sound_Controller.SetRTPCValue("heart", blood) end function Sound_Controller.PauseAll( ) SoundUtil.PauseAll() end function Sound_Controller.ResumeAll( ) SoundUtil.ResumeAll() end function Sound_Controller.StopAll( ) SoundUtil.StopAll() end function Sound_Controller.ClearAll( ) SoundUtil.ResetSoundEngine() end function Sound_Controller.PlayCGSound(obj) Sound_Controller.PlayObjSound("MUSIC_Cg_01", obj) end function Sound_Controller.PlayCommonClickSound() Sound_Controller.PlayGameSound("SFX_UI_click") end function Sound_Controller.PlayTaskFinishSound() Sound_Controller.PlayGameSound("SFX_UI_completion") end function Sound_Controller.PlayStartBattleSound(isWin) Sound_Controller.PlayGameSound("MUSIC_Combat") --Sound_Controller.SetState("MUSIC","Combat") --Sound_Controller.SetSwitch("HP", "Start", this.gameObject) Sound_Controller.PlayGameSound("SFX_ambient_main") end function Sound_Controller.SetState(stateGroup,pszState) SoundUtil.SetState(stateGroup,pszState) end function Sound_Controller.SetCameraListener(active) SoundUtil.SetCameraListener(active) if uiListener then SoundUtil.SetCameraListener(false) uiListener = false end --CameraController.Instance.MainCamera.gameObject:GetCo end function Sound_Controller.StopById(id) SoundUtil.Stop(id) end function Sound_Controller.StopByObj(obj) SoundUtil.StopAll(obj) end function Sound_Controller.StopByRenderEntity(renderEntity) if renderEntity then SoundUtil.StopAll(renderEntity) end end function Sound_Controller.StopByIdWithTime(id, stopTime) SoundUtil.Stop(id,stopTime) end function Sound_Controller.BeginLoading(loadingType) -- 当加载关卡时 暂停背景声音 --Sound_Controller.PlayGameSound("Music_pause") if loadingType ~= MDL.Loading.LoadingTypeEnum.FastFollowAssetPack then Sound_Controller.PlayGameSound("MUSIC_guodu") Sound_Controller.PlayGameSound("MUSiC_empty") if loadingType == MDL.Loading.LoadingTypeEnum.EnterWorld or loadingType == MDL.Loading.LoadingTypeEnum.InGame or loadingType == MDL.Loading.LoadingTypeEnum.PlotReview then CTL.Sound.PlayGameSound("SFX_Lowpass") end if loadingType ~= MDL.Loading.LoadingTypeEnum.EnterWorld then Sound_Controller.UnloadBankAll(false) end end end function Sound_Controller.PlayLevelSound(id)-- 当加载关卡完毕时 如果上一关和当前关声音一样 则只是恢复 不切换声音 local data = id>0 and DataSheetManager.LevelInfoData:GetDataByLevelID(id) or nil if data and not string.isNilOrEmpty(data.LevelSound) then Sound_Controller.PlayGameSound(data.LevelSound) else Sound_Controller.PlayGameSound("MUSiC_empty") ---如果没配置声音 应该停掉上一关的声音 end --Sound_Controller.PlayGameSound("Music_resume") --3v3场景限制受击声音同时播放最大数量 if BattleDataGetter.IsSyncTeamPVP() or BattleDataGetter.IsSyncSolo() then Sound_Controller.SetRTPCValue("sound_limit",0) else Sound_Controller.SetRTPCValue("sound_limit",4) end CTL.Sound.PlayGameSound("SFX_Bypass") private.PlayLevelEnvSound( ) end function private.PlayLevelEnvSound( ) --播放环境音接口 临时写的 先验证正确性和性能 local layoutData = LevelItemManager.GetLayoutData() if not layoutData then return end local soundList = {} for _,item in ipairs(layoutData.LevelItemList) do if item.LevelItemType == LuaConst.LevelItemType.SoundSource and item.ActiveDefault and item.SourceName then soundList[item.SourceName] = soundList[item.SourceName] or {} local posData = {FP.tonumberV1(item.ItemPos.x),item.ItemPosY,FP.tonumberV1(item.ItemPos.z)} table.insert(soundList[item.SourceName], posData) end end for k,v in pairs(soundList) do local obj = SoundUtil.PlayMultiplePositionsSound(k,v) table.insert(levelSoundObjList,obj) end end function Sound_Controller.ClearLevelSound( ) for i,v in ipairs(levelSoundObjList) do SoundUtil.StopAll(v) if GameAppInfo.IsVersionGreaterThan114000() then AuxUtil.ReturnSoundObject(v,true) else AuxUtil.ReturnSoundObject(v) end end levelSoundObjList = {} end function Sound_Controller.Pause(id) SoundUtil.Pause(id) end function Sound_Controller.Resume(id) SoundUtil.Resume(id) end function Sound_Controller.InitSoundVolume() if MDL.Setting.HasKey(LuaConst.SettingKeyEnum.BgmState) then local value = MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.BgmProgress) if MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.BgmState) and value > 0 then Sound_Controller.SetRTPCValue("MUSIC_Bus", value) else Sound_Controller.SetRTPCValue("MUSIC_Bus", 0) end else Sound_Controller.SetRTPCValue("MUSIC_Bus", 100) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.BgmState, true) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.BgmProgress, 100) end if MDL.Setting.HasKey(LuaConst.SettingKeyEnum.SoundState) then local value = MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.SoundProgress) if MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.SoundState) and value > 0 then Sound_Controller.SetRTPCValue("SFX_Bus", value) else Sound_Controller.SetRTPCValue("SFX_Bus", 0) end else Sound_Controller.SetRTPCValue("SFX_Bus", 100) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.SoundState, true) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.SoundProgress, 100) end if MDL.Setting.HasKey(LuaConst.SettingKeyEnum.DubState) then local value = MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.DubProgress) if MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.DubState) and value > 0 then Sound_Controller.SetRTPCValue("VO_Bus", value) else Sound_Controller.SetRTPCValue("VO_Bus", 0) end else Sound_Controller.SetRTPCValue("VO_Bus", 100) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.DubState, true) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.DubProgress, 100) end if MDL.Setting.HasKey(LuaConst.SettingKeyEnum.EnvironmentalState) then local value = MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.EnvironmentalProgress) if MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.EnvironmentalState) and value > 0 then Sound_Controller.SetRTPCValue("Amb_Bus", value) else Sound_Controller.SetRTPCValue("Amb_Bus", 0) end else Sound_Controller.SetRTPCValue("Amb_Bus", 100) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.EnvironmentalState, true) MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.EnvironmentalProgress, 100) end if GameAppInfo.IsChinesePackage() then CTL.Sound.SetSwitch("Language", "China") else CTL.Sound.SetSwitch("Language", "Foreign") end end function Sound_Controller.PlayFootSound(soundName,obj) if isStopFootSound == false then Sound_Controller.PlayObjSound(soundName,obj) end end --isBattleEnd战斗结算倒计时,最后1秒音效区分处理 function Sound_Controller.PlayCountDown(num, isBattleEnd) if num == 1 then if isBattleEnd then CTL.Sound.PlayGameSound("UI_begin") else CTL.Sound.PlayGameSound("UI_count_02") end elseif num > 1 then CTL.Sound.PlayGameSound("UI_count_01") end end function Sound_Controller.MuteSound(isMute) SoundUtil.MuteSound(isMute) end function private.SetAudioLanguage() local language = nil if Sound_Controller.CanChangeAudioLanguage() then language = MDL.Setting.GetKeyValue(LuaConst.SettingKeyEnum.DubVoiceLanguage) if string.isNilOrEmpty(language) then language = private.GetAudioLanguage() MDL.Setting.SetKeyValue(LuaConst.SettingKeyEnum.DubVoiceLanguage, language) end else language = private.GetAudioLanguage() end SoundUtil.SetAudioLanguage(language) end function private.GetAudioLanguage() local language = nil -- 默认语言是英语 目前 简繁走中文 日语走日语 其它语言英文 if Localization.IsAllChinese() then language = LocalizationCfg.LANGUAGE_SC_WORLD elseif Localization.IsJapanese() then language = LocalizationCfg.LANGUAGE_JAPANESE elseif Localization.IsVietnamese() then language = LocalizationCfg.LANGUAGE_VIETNAMESE else language = LocalizationCfg.LANGUAGE_ENGLISH end return language end -- true 音频全部卸载, false 只卸载未标记为DontunLoad的音频 function Sound_Controller.UnloadBankAll(isAll) SoundUtil.UnloadBankAll(isAll) end function Sound_Controller.CanChangeAudioLanguage() if GameAppInfo.IsChinesePackage() or GameAppInfo.IsJapanesePackage() or GameAppInfo.IsVietnamPackage() then return false else return true end end function Sound_Controller.ResumeLevelSound() local data = DataSheetManager.LevelInfoData:GetDataByLevelID(BattleDataGetter.GetLevelID()) if data and not string.isNilOrEmpty(data.LevelSound) then Sound_Controller.PlayGameSound(data.LevelSound) end end function Sound_Controller.OnMovieStop(movieName) if table.containValue(resumeMovies, movieName) then Sound_Controller.ResumeLevelSound() end end ---声音延迟播放功能 local delayQueue = {} local delayId = 0 local delayInfo = {} function private.DelayPlayDialogue(ids) local delay = private.GetDialogueDelay(ids) if delay and delay > 0 then if delayInfo.timerId then TimerUtil.Remove(delayInfo.timerId) private.PlayDialogue() end delayInfo.delay = delay delayInfo.name = ids delayInfo.timerId = TimerUtil.Interval(private.PlayDialogue, delay, 1) else Sound_Controller.CheckAndPlay(ids) end end function private.PlayDialogue() Sound_Controller.CheckAndPlay(delayInfo.name) delayInfo.timerId = nil end function private.ClearDialogueQueue() if delayInfo and delayInfo.timerId then TimerUtil.Remove(delayInfo.timerId) delayInfo.timerId = nil end Sound_Controller.PlayGameSound("dialogue_stop") end function private.GetDialogueDelay(ids) local isSuccess, data = DataSheetManager.DelayDialogue:TryGetDataByID(ids) if isSuccess then return data[Localization.GetLanguage()] end end function private.OnApplicationQuit() applicationQuitting = true end function Sound_Controller.SetPadPath(path) SoundUtil.AddBasePath(path) SoundUtil.ResetBasePath() end local viewLowPasscount = 0 function Sound_Controller.HighPass() viewLowPasscount = viewLowPasscount - 1 if viewLowPasscount <= 0 then isStopFootSound = false CTL.Sound.PlayGameSound("SFX_Bypass") viewLowPasscount = 0 end end function Sound_Controller.LowPass() if viewLowPasscount == 0 then isStopFootSound = true CTL.Sound.PlayGameSound("SFX_Lowpass") end viewLowPasscount = viewLowPasscount + 1 end function private.OnFocusOnTownHome() if viewLowPasscount > 1 then logging.error(LTAG.ERROR, "错误 全屏界面声音机制计数错误", viewLowPasscount) end end -- 播放斩杀配音 function Sound_Controller.PlayKillSound(killHeroCount) for k,v in DataSheetManager.ContinuousKillingSound:GetEnumerator() do if v.ID == killHeroCount then Sound_Controller.PlayGameSound(DataSheetManager.ContinuousKillingSound:GetDataByID(v.ID).Sound) Sound_Controller.killGapMaxCount = k == DataSheetManager.ContinuousKillingSound:GetDataCount() and 0 or -1 -- 这个目的是为了不需要在每次切关卡的时候再清理一次 elseif k == DataSheetManager.ContinuousKillingSound:GetDataCount() and killHeroCount >= v.ID then -- 达到最高后使用最高音效 if Sound_Controller.killGapMaxCount == -1 or Sound_Controller.killGapMaxCount >= 2 then Sound_Controller.PlayGameSound(DataSheetManager.ContinuousKillingSound:GetDataByID(v.ID).Sound) Sound_Controller.killGapMaxCount = 0 else Sound_Controller.killGapMaxCount = Sound_Controller.killGapMaxCount + 1 end end end end return Sound_Controller