2025-03-31 11:49:53 +08:00
|
|
|
|
using System.Collections;
|
2025-03-06 17:24:31 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
2025-03-31 11:49:53 +08:00
|
|
|
|
namespace Stary.Evo.AudioCore
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public class VoicePlayer : AbstractAudio
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
private AudioSourcePool audioSourcePool;
|
|
|
|
|
|
private AudioSource currentSource;
|
|
|
|
|
|
private Coroutine myCoroutine;
|
2025-03-07 17:52:50 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public VoicePlayer(AudioSourcePool audioSourcePool)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
this.audioSourcePool = audioSourcePool;
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// <summary>
|
2025-03-31 11:49:53 +08:00
|
|
|
|
/// 播放语音
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// </summary>
|
2025-03-31 11:49:53 +08:00
|
|
|
|
/// <param name="audioData">{[clip:音频], [volume:音量],
|
|
|
|
|
|
/// [onComplete:回调行为], [delayOnCompleteTime:延迟回调执行的时间]}</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public override void Play(AudioData audioData)
|
|
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 停止当前正在播放的语音与旧协程
|
2025-03-26 09:34:52 +08:00
|
|
|
|
Stop(new AudioData { });
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
audioData = AudioDataInitialize(audioData);
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (myCoroutine != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
CoroutineHelper.StopCoroutine(myCoroutine);
|
|
|
|
|
|
myCoroutine = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
currentSource = audioSourcePool.GetAudioSource("Voice");
|
|
|
|
|
|
if (currentSource == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
currentSource.clip = audioData.clip;
|
|
|
|
|
|
currentSource.volume = audioData.volume;
|
|
|
|
|
|
currentSource.Play();
|
|
|
|
|
|
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 使用协程处理延迟和回调
|
2025-03-26 09:34:52 +08:00
|
|
|
|
myCoroutine = CoroutineHelper.StartCoroutine(PlayVoiceCoroutine(currentSource, audioData.delayOnCompleteTime, audioData.onComplete));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-31 11:49:53 +08:00
|
|
|
|
/// 停止语音
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// </summary>
|
2025-03-31 11:49:53 +08:00
|
|
|
|
/// /// <param name="audioData">{[无可使用变量]}</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public override void Stop(AudioData audioData)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (currentSource != null && currentSource.isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentSource.Stop();
|
|
|
|
|
|
audioSourcePool.ReturnAudioSource("Voice", currentSource.gameObject);
|
|
|
|
|
|
currentSource = null;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// <summary>
|
2025-03-31 11:49:53 +08:00
|
|
|
|
/// 播放语音的协程
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
|
/// <param name="delayOnComplete"></param>
|
|
|
|
|
|
/// <param name="onComplete"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private IEnumerator PlayVoiceCoroutine(AudioSource source, float delayOnComplete, System.Action onComplete)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(source.clip.length + delayOnComplete);
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
onComplete?.Invoke();
|
|
|
|
|
|
audioSourcePool.ReturnAudioSource("Voice", source.gameObject);
|
|
|
|
|
|
currentSource = null;
|
|
|
|
|
|
myCoroutine = null;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|