1111
This commit is contained in:
@@ -2,19 +2,17 @@
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo.AudioCore
|
||||
{
|
||||
public class VoicePlayer : AbstractAudio
|
||||
public class VoicePlayer : AbstractAudio, IDisposable
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
private AudioSource currentSource;
|
||||
private Coroutine myCoroutine;
|
||||
private CancellationTokenSource cancelTokenSource;
|
||||
public VoicePlayer(AudioSourcePool audioSourcePool)
|
||||
private AudioSourceToken currentSource;
|
||||
|
||||
public VoicePlayer(IResources resources) : base(resources)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -24,80 +22,12 @@ namespace Stary.Evo.AudioCore
|
||||
/// [onComplete:回调行为], [delayOnCompleteTime:延迟回调执行的时间],
|
||||
/// [is3DAudio:是否3D音频], [audio3DPosition:3D音频位置],
|
||||
/// [audio3DMaxDistance:3D音频最大距离]}</param>
|
||||
public override void Play(AudioData audioData)
|
||||
public override async void Play(AudioData audioData)
|
||||
{
|
||||
// 停止当前正在播放的语音与旧协程,或旧异步
|
||||
Stop();
|
||||
|
||||
audioData = AudioDataInitialize(audioData);
|
||||
|
||||
if (myCoroutine != null)
|
||||
{
|
||||
CoroutineHelper.Instance.StopCoroutine(myCoroutine);
|
||||
myCoroutine = null;
|
||||
}
|
||||
currentSource = audioSourcePool.GetAudioSource("Voice");
|
||||
if (currentSource == null) return;
|
||||
|
||||
currentSource.clip = audioData.clip;
|
||||
currentSource.volume = audioData.volume;
|
||||
|
||||
// 设置2D与3D音频
|
||||
if (audioData.is3DAudio)
|
||||
{
|
||||
currentSource.transform.position = audioData.audio3DPosition;
|
||||
currentSource.spatialBlend = 1;
|
||||
currentSource.minDistance = 1f;
|
||||
if (audioData.audio3DMaxDistance != 0)
|
||||
{
|
||||
currentSource.maxDistance = audioData.audio3DMaxDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 默认3D最大距离为3米
|
||||
currentSource.maxDistance = 3f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentSource.transform.position = Vector3.zero;
|
||||
currentSource.spatialBlend = 0;
|
||||
currentSource.minDistance = 1f;
|
||||
currentSource.maxDistance = 500f;
|
||||
}
|
||||
|
||||
currentSource.Play();
|
||||
|
||||
AudioSourceToken audioSourceToken = await PlayAudio(audioData);
|
||||
// 使用协程处理延迟和回调
|
||||
myCoroutine = CoroutineHelper.Instance.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.Instance.StopCoroutine(myCoroutine);
|
||||
myCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 停掉异步任务
|
||||
if (cancelTokenSource != null && cancelTokenSource.IsCancellationRequested)
|
||||
{
|
||||
cancelTokenSource .Cancel();
|
||||
cancelTokenSource.Dispose();
|
||||
cancelTokenSource = null;
|
||||
}
|
||||
PlayAudioAWait(audioSourceToken,
|
||||
audioData.delayOnCompleteTime, audioData.onComplete);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,59 +38,108 @@ namespace Stary.Evo.AudioCore
|
||||
/// [is3DAudio:是否3D音频], [audio3DPosition:3D音频位置],
|
||||
/// [audio3DMaxDistance:3D音频最大距离],
|
||||
/// [cancellationToken:异步取消令牌]}</param>
|
||||
public async Task PlayAsync(AudioData audioData)
|
||||
public override async UniTask PlayAsync(AudioData audioData)
|
||||
{
|
||||
AudioSourceToken audioSourceToken = await PlayAudio(audioData);
|
||||
await PlayAudioAWait(audioSourceToken, audioData.delayOnCompleteTime);
|
||||
}
|
||||
|
||||
|
||||
private async UniTask<AudioSourceToken> PlayAudio(AudioData audioData)
|
||||
{
|
||||
if (Resources == null)
|
||||
{
|
||||
Debug.LogError("AudioCoreManager: Resources is null, please initialize it first.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 停止当前正在播放的语音与旧协程,或旧异步
|
||||
Stop();
|
||||
audioData = AudioDataInitialize(audioData);
|
||||
currentSource = audioSourcePool.GetAudioSource("Voice");
|
||||
if (currentSource == null) return;
|
||||
audioData = Initialize(audioData);
|
||||
if (!string.IsNullOrEmpty(audioData.packageName) && !string.IsNullOrEmpty(audioData.packageName))
|
||||
{
|
||||
var clip = await Resources.LoadAssetAsync<AudioClip>(audioData.packageName, audioData.assetName);
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogErrorFormat($"从资源包【{audioData.packageName}】加载音频片段【{audioData.assetName}】失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
audioData.clip = clip;
|
||||
}
|
||||
}
|
||||
|
||||
currentSource.clip = audioData.clip;
|
||||
currentSource.volume = audioData.volume;
|
||||
if (audioData.clip == null)
|
||||
{
|
||||
Debug.LogError("播放的音效音频片段为空");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
AudioSourceToken audioSourceToken = AudioSourcePool.Spawn();
|
||||
if (audioSourceToken == null)
|
||||
{
|
||||
Debug.LogError("音效池为空,无法播放音效");
|
||||
return null;
|
||||
}
|
||||
|
||||
currentSource = audioSourceToken;
|
||||
|
||||
currentSource.source.clip = audioData.clip;
|
||||
currentSource.source.volume = audioData.volume;
|
||||
|
||||
// 设置2D与3D音频
|
||||
if (audioData.is3DAudio)
|
||||
{
|
||||
currentSource.transform.position = audioData.audio3DPosition;
|
||||
currentSource.spatialBlend = 1;
|
||||
currentSource.minDistance = 1f;
|
||||
currentSource.source.transform.position = audioData.audio3DPosition;
|
||||
currentSource.source.spatialBlend = 1;
|
||||
currentSource.source.minDistance = 1f;
|
||||
if (audioData.audio3DMaxDistance != 0)
|
||||
{
|
||||
currentSource.maxDistance = audioData.audio3DMaxDistance;
|
||||
currentSource.source.maxDistance = audioData.audio3DMaxDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 默认3D最大距离为3米
|
||||
currentSource.maxDistance = 3f;
|
||||
currentSource.source.maxDistance = 3f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentSource.transform.position = Vector3.zero;
|
||||
currentSource.spatialBlend = 0;
|
||||
currentSource.minDistance = 1f;
|
||||
currentSource.maxDistance = 500f;
|
||||
currentSource.source.transform.position = Vector3.zero;
|
||||
currentSource.source.spatialBlend = 0;
|
||||
currentSource.source.minDistance = 1f;
|
||||
currentSource.source.maxDistance = 500f;
|
||||
}
|
||||
|
||||
currentSource.Play();
|
||||
await Task.Delay(TimeSpan.FromSeconds(audioData.clip.length), audioData.cancellationToken);
|
||||
currentSource.source.Play();
|
||||
|
||||
return audioSourceToken;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
/// /// <param name="audioData">{[无可使用变量]}</param>
|
||||
public override void Stop()
|
||||
{
|
||||
yield return new WaitForSeconds(source.clip.length + delayOnComplete);
|
||||
audioSourcePool.ReturnAudioSource("Voice", source.gameObject);
|
||||
onComplete?.Invoke();
|
||||
currentSource = null;
|
||||
myCoroutine = null;
|
||||
// 停止当前协程
|
||||
if (currentSource != null && currentSource.source.isPlaying)
|
||||
{
|
||||
currentSource.source.Stop();
|
||||
AudioSourcePool.RecycleSpawn(currentSource);
|
||||
currentSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void StopAll()
|
||||
{
|
||||
AudioSourcePool.RecycleAll();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
AudioSourcePool.RecycleAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user