113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace Stary.Evo.AudioCore
|
|
{
|
|
public class VoicePlayer : AbstractAudio
|
|
{
|
|
private AudioSourcePool audioSourcePool;
|
|
private AudioSource currentSource;
|
|
private Coroutine myCoroutine;
|
|
private CancellationTokenSource cancelTokenSource;
|
|
public VoicePlayer(AudioSourcePool audioSourcePool)
|
|
{
|
|
this.audioSourcePool = audioSourcePool;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放语音
|
|
/// </summary>
|
|
/// <param name="audioData">{[clip:音频], [volume:音量],
|
|
/// [onComplete:回调行为], [delayOnCompleteTime:延迟回调执行的时间]}</param>
|
|
public override void Play(AudioData audioData)
|
|
{
|
|
// 停止当前正在播放的语音与旧协程,或旧异步
|
|
Stop();
|
|
|
|
audioData = AudioDataInitialize(audioData);
|
|
|
|
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();
|
|
|
|
// 使用协程处理延迟和回调
|
|
myCoroutine = CoroutineHelper.StartCoroutine(PlayVoiceCoroutine(currentSource, audioData.delayOnCompleteTime, audioData.onComplete));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止语音
|
|
/// </summary>
|
|
/// /// <param name="audioData">{[无可使用变量]}</param>
|
|
public override void Stop()
|
|
{
|
|
// 停止当前协程
|
|
if (currentSource != null && currentSource.isPlaying)
|
|
{
|
|
currentSource.Stop();
|
|
audioSourcePool.ReturnAudioSource("Voice", currentSource.gameObject);
|
|
currentSource = null;
|
|
if (myCoroutine != null)
|
|
{
|
|
CoroutineHelper.StopCoroutine(myCoroutine);
|
|
myCoroutine = null;
|
|
}
|
|
}
|
|
|
|
// 停掉异步任务
|
|
if (cancelTokenSource != null && cancelTokenSource.IsCancellationRequested)
|
|
{
|
|
cancelTokenSource .Cancel();
|
|
cancelTokenSource.Dispose();
|
|
cancelTokenSource = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步播放语音
|
|
/// </summary>
|
|
/// <param name="audioClip"></param>
|
|
/// <param name="volume"></param>
|
|
public async Task Play(AudioClip audioClip, float volume)
|
|
{
|
|
// 停止当前正在播放的语音与旧协程,或旧异步
|
|
Stop();
|
|
|
|
currentSource = audioSourcePool.GetAudioSource("Voice");
|
|
if (currentSource == null) return;
|
|
|
|
currentSource.clip = audioClip;
|
|
currentSource.volume = volume;
|
|
currentSource.Play();
|
|
|
|
cancelTokenSource = new CancellationTokenSource();
|
|
await Task.Delay(TimeSpan.FromSeconds(audioClip.length),cancelTokenSource.Token);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放语音的协程
|
|
/// </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);
|
|
audioSourcePool.ReturnAudioSource("Voice", source.gameObject);
|
|
onComplete?.Invoke();
|
|
currentSource = null;
|
|
myCoroutine = null;
|
|
}
|
|
}
|
|
} |