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 MusicPlayer : AbstractAudio
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
private AudioSourcePool audioSourcePool;
|
|
|
|
|
|
private AudioSource audioSource1;
|
|
|
|
|
|
private AudioSource audioSource2;
|
|
|
|
|
|
private AudioSource currentAudioSource;
|
2025-05-07 14:34:07 +08:00
|
|
|
|
private Coroutine myCoroutine;
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public MusicPlayer(AudioSourcePool audioSourcePool)
|
2025-03-07 15:21:24 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
this.audioSourcePool = audioSourcePool;
|
2025-03-07 15:21:24 +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:音量], [fadeDuration:自然过渡时间]}</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public override void Play(AudioData audioData)
|
2025-03-07 15:21:24 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
audioData = AudioDataInitialize(audioData);
|
|
|
|
|
|
if (audioSource1 == null)
|
2025-03-07 15:21:24 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
audioSource1 = audioSourcePool.GetAudioSource("Music");
|
|
|
|
|
|
audioSource1.clip = audioData.clip;
|
|
|
|
|
|
audioSource1.volume = audioData.volume;
|
|
|
|
|
|
currentAudioSource = audioSource1;
|
2025-03-07 15:21:24 +08:00
|
|
|
|
currentAudioSource.Play();
|
2025-04-29 11:02:18 +08:00
|
|
|
|
FadeMusic(audioSource1, audioData.fadeDuration, audioSource2);
|
2025-03-07 15:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (audioSource2 == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
audioSource2 = audioSourcePool.GetAudioSource("Music");
|
|
|
|
|
|
audioSource2.clip = audioData.clip;
|
|
|
|
|
|
audioSource2.volume = audioData.volume;
|
|
|
|
|
|
currentAudioSource = audioSource2;
|
|
|
|
|
|
currentAudioSource.Play();
|
2025-04-29 11:02:18 +08:00
|
|
|
|
FadeMusic(audioSource2, audioData.fadeDuration, audioSource1);
|
2025-03-26 09:34:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-10 11:57:13 +08:00
|
|
|
|
Debug.LogWarning("UnityEvo:已同时存在两个背景乐在切换");
|
2025-03-26 09:34:52 +08:00
|
|
|
|
}
|
2025-03-07 15:21:24 +08:00
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
}
|
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">{[fadeDuration:自然过渡时间]}</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
public override void Stop(AudioData audioData)
|
2025-03-07 15:21:24 +08:00
|
|
|
|
{
|
2025-04-29 11:02:18 +08:00
|
|
|
|
FadeAllMusic();
|
2025-03-07 15:21:24 +08:00
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-05-07 14:34:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置背景音乐音量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="audioData"></param>
|
|
|
|
|
|
public void SetMusicVolume(AudioData audioData)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(currentAudioSource == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (myCoroutine != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
CoroutineHelper.StopCoroutine(myCoroutine);
|
|
|
|
|
|
myCoroutine = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
myCoroutine = CoroutineHelper.StartCoroutine(SetMusicVolume(audioData.fadeDuration, audioData.volume));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置自然过渡到指定音量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fadeDuration"></param>
|
|
|
|
|
|
/// <param name="targetVolume"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private IEnumerator SetMusicVolume(float fadeDuration, float targetVolume)
|
|
|
|
|
|
{
|
|
|
|
|
|
float startVolume = currentAudioSource.volume;
|
|
|
|
|
|
while (currentAudioSource.volume != targetVolume)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentAudioSource.volume > targetVolume)
|
|
|
|
|
|
{
|
2025-06-23 18:03:56 +08:00
|
|
|
|
currentAudioSource.volume -= (startVolume-targetVolume) * Time.deltaTime / fadeDuration;
|
2025-05-07 14:34:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-06-23 18:03:56 +08:00
|
|
|
|
currentAudioSource.volume += (targetVolume-startVolume) * Time.deltaTime / fadeDuration;
|
2025-05-07 14:34:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Mathf.Abs(currentAudioSource.volume - targetVolume) < 0.01f)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentAudioSource.volume = targetVolume;
|
|
|
|
|
|
}
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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="source1">播放的音频</param>
|
|
|
|
|
|
/// <param name="fadeDuration">变化时间</param>
|
|
|
|
|
|
/// <param name="source2">停止的音频</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
/// <returns></returns>
|
2025-04-29 11:02:18 +08:00
|
|
|
|
private void FadeMusic(AudioSource source1, float fadeDuration, AudioSource source2 = null)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-04-29 11:02:18 +08:00
|
|
|
|
CoroutineHelper.StartCoroutine(FadeInMusic(source1, fadeDuration));
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (source2 != null)
|
|
|
|
|
|
{
|
2025-04-29 11:02:18 +08:00
|
|
|
|
CoroutineHelper.StartCoroutine(FadeOutMusic(source2, fadeDuration));
|
2025-03-26 09:34:52 +08:00
|
|
|
|
}
|
2025-03-07 15:21:24 +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="fadeDuration"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private IEnumerator FadeOutMusic(AudioSource source, float fadeDuration)
|
2025-03-07 15:21:24 +08:00
|
|
|
|
{
|
2025-04-29 11:02:18 +08:00
|
|
|
|
if (source == null) yield break;
|
2025-03-26 09:34:52 +08:00
|
|
|
|
float startVolume = source.volume;
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
while (source.volume > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
source.volume -= startVolume * Time.deltaTime / fadeDuration;
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
source.Stop();
|
2025-04-29 11:02:18 +08:00
|
|
|
|
if (audioSource1 == source)
|
|
|
|
|
|
{
|
|
|
|
|
|
audioSource1 = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (audioSource2 == source)
|
|
|
|
|
|
{
|
|
|
|
|
|
audioSource2 = null;
|
|
|
|
|
|
}
|
2025-03-26 09:34:52 +08:00
|
|
|
|
audioSourcePool.ReturnAudioSource("Music", source.gameObject);
|
2025-04-29 11:02:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void FadeAllMusic()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentAudioSource != null && currentAudioSource.isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentAudioSource == audioSource1)
|
|
|
|
|
|
{
|
|
|
|
|
|
audioSource1 = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
audioSource2 = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
audioSourcePool.ReturnAudioSource("Music", currentAudioSource.gameObject);
|
|
|
|
|
|
currentAudioSource = null;
|
2025-03-26 09:34:52 +08:00
|
|
|
|
}
|
2025-04-29 11:02:18 +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="fadeDuration"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private IEnumerator FadeInMusic(AudioSource source, float fadeDuration)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
float targetVolume = source.volume;
|
|
|
|
|
|
source.volume = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (source.volume < targetVolume)
|
|
|
|
|
|
{
|
|
|
|
|
|
source.volume += targetVolume * Time.deltaTime / fadeDuration;
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|