464 lines
14 KiB
C#
464 lines
14 KiB
C#
|
|
// using System;
|
|||
|
|
// using System.Collections;
|
|||
|
|
// using UnityEngine;
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 音频管理服务
|
|||
|
|
// /// 负责处理报警音频播放、静音控制和音量管理
|
|||
|
|
// /// </summary>
|
|||
|
|
// public class AudioService : MonoBehaviour, IService
|
|||
|
|
// {
|
|||
|
|
// public static AudioService Instance { get; private set; }
|
|||
|
|
|
|||
|
|
// [Header("音频设置")]
|
|||
|
|
// public AudioSource AlarmAudioSource;
|
|||
|
|
// public AudioClip AlarmAudioClip;
|
|||
|
|
// public float DefaultVolume = 0.8f;
|
|||
|
|
// public bool Loop = true;
|
|||
|
|
|
|||
|
|
// [Header("音量设置")]
|
|||
|
|
// [Range(0f, 1f)]
|
|||
|
|
// public float MasterVolume = 1.0f;
|
|||
|
|
// [Range(0f, 1f)]
|
|||
|
|
// public float AlarmVolume = 0.8f;
|
|||
|
|
|
|||
|
|
// // 状态控制
|
|||
|
|
// public bool IsMuted { get; private set; }
|
|||
|
|
// public bool IsPlayingAlarm { get; private set; }
|
|||
|
|
|
|||
|
|
// // 事件
|
|||
|
|
// public event Action<bool> OnMuteStateChanged;
|
|||
|
|
// public event Action<float> OnVolumeChanged;
|
|||
|
|
// public event Action<bool> OnAlarmPlayStateChanged;
|
|||
|
|
// private Coroutine _fadeCoroutine;
|
|||
|
|
|
|||
|
|
// // 设置保存键
|
|||
|
|
// private const string MASTER_VOLUME_KEY = "AudioService_MasterVolume";
|
|||
|
|
// private const string ALARM_VOLUME_KEY = "AudioService_AlarmVolume";
|
|||
|
|
// private const string MUTE_STATE_KEY = "AudioService_MuteState";
|
|||
|
|
|
|||
|
|
// void Awake()
|
|||
|
|
// {
|
|||
|
|
// if (Instance == null)
|
|||
|
|
// {
|
|||
|
|
// Instance = this;
|
|||
|
|
// DontDestroyOnLoad(gameObject);
|
|||
|
|
// InitializeAudioService();
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// Destroy(gameObject);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// void Start()
|
|||
|
|
// {
|
|||
|
|
// RegisterWithServiceLocator();
|
|||
|
|
// ConnectToDCSAlarmManager();
|
|||
|
|
// LoadAudioSettings();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void InitializeAudioService()
|
|||
|
|
// {
|
|||
|
|
// // 创建AudioSource如果不存在
|
|||
|
|
// if (AlarmAudioSource == null)
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioSource = gameObject.AddComponent<AudioSource>();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 尝试加载默认报警音频
|
|||
|
|
// LoadDefaultAlarmAudio();
|
|||
|
|
|
|||
|
|
// // 配置AudioSource
|
|||
|
|
// ConfigureAudioSource();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void LoadDefaultAlarmAudio()
|
|||
|
|
// {
|
|||
|
|
// // 从Resources/Audio/目录加载DiDiDiDi音频文件
|
|||
|
|
// if (AlarmAudioClip == null)
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioClip = Resources.Load<AudioClip>("Audio/DiDiDiDi");
|
|||
|
|
// if (AlarmAudioClip != null)
|
|||
|
|
// {
|
|||
|
|
// Debug.Log("AudioService: 成功加载默认报警音频 DiDiDiDi");
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// Debug.LogWarning("AudioService: 未找到Resources/Audio/DiDiDiDi音频文件");
|
|||
|
|
// // 尝试其他可能的文件名或路径
|
|||
|
|
// TryLoadAlternativeAudioFiles();
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void TryLoadAlternativeAudioFiles()
|
|||
|
|
// {
|
|||
|
|
// string[] possiblePaths = {
|
|||
|
|
// "Audio/DiDiDiDi",
|
|||
|
|
// "Audio/didididi",
|
|||
|
|
// "Audio/alarm",
|
|||
|
|
// "Audio/Alarm",
|
|||
|
|
// "Audio/beep",
|
|||
|
|
// "DiDiDiDi"
|
|||
|
|
// };
|
|||
|
|
|
|||
|
|
// foreach (string path in possiblePaths)
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioClip = Resources.Load<AudioClip>(path);
|
|||
|
|
// if (AlarmAudioClip != null)
|
|||
|
|
// {
|
|||
|
|
// Debug.Log($"AudioService: 找到替代报警音频文件: {path}");
|
|||
|
|
// break;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// if (AlarmAudioClip == null)
|
|||
|
|
// {
|
|||
|
|
// Debug.LogError("AudioService: 无法找到任何报警音频文件,请确保音频文件位于Resources/Audio/目录下");
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void ConfigureAudioSource()
|
|||
|
|
// {
|
|||
|
|
// if (AlarmAudioSource != null)
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioSource.clip = AlarmAudioClip;
|
|||
|
|
// AlarmAudioSource.loop = Loop;
|
|||
|
|
// AlarmAudioSource.playOnAwake = false;
|
|||
|
|
// AlarmAudioSource.volume = DefaultVolume;
|
|||
|
|
// AlarmAudioSource.pitch = 1.0f;
|
|||
|
|
// AlarmAudioSource.spatialBlend = 0f; // 2D音频
|
|||
|
|
// AlarmAudioSource.rolloffMode = AudioRolloffMode.Logarithmic;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void RegisterWithServiceLocator()
|
|||
|
|
// {
|
|||
|
|
// if (!ServiceLocator.IsRegistered<AudioService>())
|
|||
|
|
// {
|
|||
|
|
// ServiceLocator.Register<AudioService>(this);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void ConnectToDCSAlarmManager()
|
|||
|
|
// {
|
|||
|
|
// // 订阅DCSAlarmManager的报警事件
|
|||
|
|
// DCSAlarmManager.OnAlarmRaised += OnAlarmRaised;
|
|||
|
|
// DCSAlarmManager.OnMuteStateChanged += OnMuteStateChangedFromDCS;
|
|||
|
|
// Debug.Log("AudioService: 已连接到DCSAlarmManager");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void OnMuteStateChangedFromDCS(bool isMuted)
|
|||
|
|
// {
|
|||
|
|
// // 同步静音状态
|
|||
|
|
// SetMute(isMuted);
|
|||
|
|
// Debug.Log($"AudioService: 从DCSAlarmManager同步静音状态 - {isMuted}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void OnAlarmRaised(AlarmPriority priority, string reason)
|
|||
|
|
// {
|
|||
|
|
// // 创建临时AlarmEvent对象来兼容现有逻辑
|
|||
|
|
// var alarmEvent = new AlarmEvent
|
|||
|
|
// {
|
|||
|
|
// Id = UnityEngine.Random.Range(1000, 9999),
|
|||
|
|
// Priority = priority,
|
|||
|
|
// Reason = reason,
|
|||
|
|
// Time = System.DateTime.Now
|
|||
|
|
// };
|
|||
|
|
|
|||
|
|
// // 根据报警优先级决定是否播放音频
|
|||
|
|
// bool shouldPlayAudio = ShouldPlayAlarmAudio(alarmEvent);
|
|||
|
|
|
|||
|
|
// if (shouldPlayAudio && !IsMuted)
|
|||
|
|
// {
|
|||
|
|
// PlayAlarmAudio(priority);
|
|||
|
|
// Debug.Log($"AudioService: 播放报警音频 - {reason} (优先级: {priority})");
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// Debug.Log($"AudioService: 报警音频被阻止播放 - 静音状态: {IsMuted}, 应播放: {shouldPlayAudio}");
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private bool ShouldPlayAlarmAudio(AlarmEvent alarmEvent)
|
|||
|
|
// {
|
|||
|
|
// // 可以根据报警优先级决定是否播放音频
|
|||
|
|
// return alarmEvent.Priority switch
|
|||
|
|
// {
|
|||
|
|
// AlarmPriority.High => true, // 高优先级总是播放
|
|||
|
|
// AlarmPriority.Medium => true, // 中优先级播放
|
|||
|
|
// AlarmPriority.Low => false, // 低优先级不播放音频,只显示
|
|||
|
|
// _ => false
|
|||
|
|
// };
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 播放报警音频
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void PlayAlarmAudio(AlarmPriority priority = AlarmPriority.Medium)
|
|||
|
|
// {
|
|||
|
|
// if (AlarmAudioSource == null || AlarmAudioClip == null)
|
|||
|
|
// {
|
|||
|
|
// Debug.LogWarning("AudioService: 无法播放报警音频 - AudioSource或AudioClip为空");
|
|||
|
|
// return;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// if (IsMuted)
|
|||
|
|
// {
|
|||
|
|
// Debug.Log("AudioService: 音频已静音,跳过播放");
|
|||
|
|
// return;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 设置音量(根据优先级调整)
|
|||
|
|
// float volumeMultiplier = GetVolumeMultiplierForPriority(priority);
|
|||
|
|
// AlarmAudioSource.volume = MasterVolume * AlarmVolume * volumeMultiplier;
|
|||
|
|
|
|||
|
|
// // 如果已在播放,先停止
|
|||
|
|
// if (IsPlayingAlarm)
|
|||
|
|
// {
|
|||
|
|
// StopAlarmAudio();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 开始播放
|
|||
|
|
// AlarmAudioSource.Play();
|
|||
|
|
// IsPlayingAlarm = true;
|
|||
|
|
// OnAlarmPlayStateChanged?.Invoke(true);
|
|||
|
|
|
|||
|
|
// Debug.Log($"AudioService: 开始播放报警音频,优先级: {priority}, 音量: {AlarmAudioSource.volume:F2}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private float GetVolumeMultiplierForPriority(AlarmPriority priority)
|
|||
|
|
// {
|
|||
|
|
// return priority switch
|
|||
|
|
// {
|
|||
|
|
// AlarmPriority.High => 1.0f, // 高优先级最大音量
|
|||
|
|
// AlarmPriority.Medium => 0.8f, // 中优先级80%音量
|
|||
|
|
// AlarmPriority.Low => 0.6f, // 低优先级60%音量
|
|||
|
|
// _ => 0.8f
|
|||
|
|
// };
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 停止报警音频
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void StopAlarmAudio(bool fadeOut = false)
|
|||
|
|
// {
|
|||
|
|
// if (!IsPlayingAlarm || AlarmAudioSource == null)
|
|||
|
|
// return;
|
|||
|
|
|
|||
|
|
// if (fadeOut)
|
|||
|
|
// {
|
|||
|
|
// StartFadeOut();
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioSource.Stop();
|
|||
|
|
// IsPlayingAlarm = false;
|
|||
|
|
// OnAlarmPlayStateChanged?.Invoke(false);
|
|||
|
|
// Debug.Log("AudioService: 停止播放报警音频");
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 渐出音频
|
|||
|
|
// /// </summary>
|
|||
|
|
// private void StartFadeOut()
|
|||
|
|
// {
|
|||
|
|
// if (_fadeCoroutine != null)
|
|||
|
|
// {
|
|||
|
|
// StopCoroutine(_fadeCoroutine);
|
|||
|
|
// }
|
|||
|
|
// _fadeCoroutine = StartCoroutine(FadeOutCoroutine());
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private IEnumerator FadeOutCoroutine()
|
|||
|
|
// {
|
|||
|
|
// float startVolume = AlarmAudioSource.volume;
|
|||
|
|
// float fadeTime = 1.0f;
|
|||
|
|
// float elapsed = 0;
|
|||
|
|
|
|||
|
|
// while (elapsed < fadeTime && AlarmAudioSource.isPlaying)
|
|||
|
|
// {
|
|||
|
|
// elapsed += Time.deltaTime;
|
|||
|
|
// AlarmAudioSource.volume = Mathf.Lerp(startVolume, 0, elapsed / fadeTime);
|
|||
|
|
// yield return null;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// AlarmAudioSource.Stop();
|
|||
|
|
// AlarmAudioSource.volume = startVolume; // 恢复原始音量
|
|||
|
|
// IsPlayingAlarm = false;
|
|||
|
|
// OnAlarmPlayStateChanged?.Invoke(false);
|
|||
|
|
|
|||
|
|
// Debug.Log("AudioService: 报警音频渐出完成");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// #region 音量和静音控制
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 设置主音量
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetMasterVolume(float volume)
|
|||
|
|
// {
|
|||
|
|
// MasterVolume = Mathf.Clamp01(volume);
|
|||
|
|
// UpdateAudioSourceVolume();
|
|||
|
|
// SaveAudioSettings();
|
|||
|
|
// OnVolumeChanged?.Invoke(MasterVolume);
|
|||
|
|
|
|||
|
|
// Debug.Log($"AudioService: 主音量设置为 {MasterVolume:F2}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 设置报警音量
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetAlarmVolume(float volume)
|
|||
|
|
// {
|
|||
|
|
// AlarmVolume = Mathf.Clamp01(volume);
|
|||
|
|
// UpdateAudioSourceVolume();
|
|||
|
|
// SaveAudioSettings();
|
|||
|
|
// OnVolumeChanged?.Invoke(AlarmVolume);
|
|||
|
|
|
|||
|
|
// Debug.Log($"AudioService: 报警音量设置为 {AlarmVolume:F2}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 切换静音状态
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void ToggleMute()
|
|||
|
|
// {
|
|||
|
|
// SetMute(!IsMuted);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 设置静音状态
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetMute(bool mute)
|
|||
|
|
// {
|
|||
|
|
// bool previousState = IsMuted;
|
|||
|
|
// IsMuted = mute;
|
|||
|
|
// // 立即停止当前播放的报警音频(如果有)
|
|||
|
|
// if (mute && IsPlayingAlarm)
|
|||
|
|
// {
|
|||
|
|
// StopAlarmAudio(true); // 渐出停止
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 同步到DCSAlarmManager的静音状态
|
|||
|
|
// if (DCSAlarmManager.Instance != null && DCSAlarmManager.Instance.IsMuted != mute)
|
|||
|
|
// {
|
|||
|
|
// DCSAlarmManager.Instance.SetMute(mute);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// SaveAudioSettings();
|
|||
|
|
// OnMuteStateChanged?.Invoke(IsMuted);
|
|||
|
|
|
|||
|
|
// Debug.Log($"AudioService: 静音状态 {previousState} -> {IsMuted}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 临时静音(指定时长)
|
|||
|
|
// /// </summary>
|
|||
|
|
// public void SetMute(bool mute, int durationInMinutes)
|
|||
|
|
// {
|
|||
|
|
// SetMute(mute);
|
|||
|
|
|
|||
|
|
// if (mute && durationInMinutes > 0)
|
|||
|
|
// {
|
|||
|
|
// StartCoroutine(UnmuteAfterDelay(durationInMinutes));
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// // 同步到DCSAlarmManager
|
|||
|
|
// if (DCSAlarmManager.Instance != null)
|
|||
|
|
// {
|
|||
|
|
// DCSAlarmManager.Instance.SetMute(mute, durationInMinutes);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private IEnumerator UnmuteAfterDelay(int minutes)
|
|||
|
|
// {
|
|||
|
|
// yield return new WaitForSeconds(minutes * 60f);
|
|||
|
|
// SetMute(false);
|
|||
|
|
// Debug.Log($"AudioService: {minutes}分钟静音时间结束,自动恢复音频");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void UpdateAudioSourceVolume()
|
|||
|
|
// {
|
|||
|
|
// if (AlarmAudioSource != null)
|
|||
|
|
// {
|
|||
|
|
// // 只在播放时更新音量,避免影响未播放的状态
|
|||
|
|
// if (IsPlayingAlarm)
|
|||
|
|
// {
|
|||
|
|
// AlarmAudioSource.volume = MasterVolume * AlarmVolume;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// #endregion
|
|||
|
|
|
|||
|
|
// #region 设置保存和加载
|
|||
|
|
|
|||
|
|
// private void SaveAudioSettings()
|
|||
|
|
// {
|
|||
|
|
// PlayerPrefs.SetFloat(MASTER_VOLUME_KEY, MasterVolume);
|
|||
|
|
// PlayerPrefs.SetFloat(ALARM_VOLUME_KEY, AlarmVolume);
|
|||
|
|
// PlayerPrefs.SetInt(MUTE_STATE_KEY, IsMuted ? 1 : 0);
|
|||
|
|
// PlayerPrefs.Save();
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// private void LoadAudioSettings()
|
|||
|
|
// {
|
|||
|
|
// MasterVolume = PlayerPrefs.GetFloat(MASTER_VOLUME_KEY, 1.0f);
|
|||
|
|
// AlarmVolume = PlayerPrefs.GetFloat(ALARM_VOLUME_KEY, 0.8f);
|
|||
|
|
// IsMuted = PlayerPrefs.GetInt(MUTE_STATE_KEY, 0) == 1;
|
|||
|
|
|
|||
|
|
// UpdateAudioSourceVolume();
|
|||
|
|
|
|||
|
|
// Debug.Log($"AudioService: 加载音频设置 - 主音量: {MasterVolume:F2}, 报警音量: {AlarmVolume:F2}, 静音: {IsMuted}");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// #endregion
|
|||
|
|
|
|||
|
|
// #region 测试和调试方法
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 测试播放报警音频
|
|||
|
|
// /// </summary>
|
|||
|
|
// [ContextMenu("测试播放报警音频")]
|
|||
|
|
// public void TestPlayAlarm()
|
|||
|
|
// {
|
|||
|
|
// PlayAlarmAudio(AlarmPriority.High);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 测试停止报警音频
|
|||
|
|
// /// </summary>
|
|||
|
|
// [ContextMenu("测试停止报警音频")]
|
|||
|
|
// public void TestStopAlarm()
|
|||
|
|
// {
|
|||
|
|
// StopAlarmAudio(true);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// /// <summary>
|
|||
|
|
// /// 获取音频状态信息
|
|||
|
|
// /// </summary>
|
|||
|
|
// public string GetAudioStatusInfo()
|
|||
|
|
// {
|
|||
|
|
// return $"主音量: {MasterVolume:F2}, 报警音量: {AlarmVolume:F2}, 静音: {IsMuted}, 正在播放: {IsPlayingAlarm}, " +
|
|||
|
|
// $"音频文件: {(AlarmAudioClip != null ? AlarmAudioClip.name : "无")}";
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// #endregion
|
|||
|
|
|
|||
|
|
// void OnDestroy()
|
|||
|
|
// {
|
|||
|
|
// // 注销DCSAlarmManager事件
|
|||
|
|
// DCSAlarmManager.OnAlarmRaised -= OnAlarmRaised;
|
|||
|
|
// DCSAlarmManager.OnMuteStateChanged -= OnMuteStateChangedFromDCS;
|
|||
|
|
|
|||
|
|
// if (_fadeCoroutine != null)
|
|||
|
|
// {
|
|||
|
|
// StopCoroutine(_fadeCoroutine);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// SaveAudioSettings();
|
|||
|
|
// }
|
|||
|
|
// }
|