AudioCore0.11
This commit is contained in:
@@ -4,38 +4,84 @@ using UnityEngine;
|
||||
public class MusicPlayer
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
private AudioSource audioSource1;
|
||||
private AudioSource audioSource2;
|
||||
private AudioSource currentAudioSource;
|
||||
|
||||
public MusicPlayer(AudioSourcePool audioSourcePool)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
}
|
||||
|
||||
public void Play(AudioClip clip, float fadeDuration)
|
||||
/// <summary>
|
||||
/// 播放背景音乐
|
||||
/// </summary>
|
||||
/// <param name="clip">音频</param>
|
||||
/// <param name="volume">音量</param>
|
||||
/// <param name="fadeDuration">自然过渡时间</param>
|
||||
public void Play(AudioClip clip, float volume, float fadeDuration)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("Music");
|
||||
if (source == null) return;
|
||||
|
||||
CoroutineHelper.StartCoroutine(FadeMusic(source, clip, fadeDuration));
|
||||
if(audioSource1 == null)
|
||||
{
|
||||
audioSource1 = audioSourcePool.GetAudioSource("Music");
|
||||
audioSource1.clip = clip;
|
||||
audioSource1.volume = volume;
|
||||
currentAudioSource = audioSource1;
|
||||
currentAudioSource.Play();
|
||||
CoroutineHelper.StartCoroutine(FadeMusic(audioSource1, fadeDuration, audioSource2));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (audioSource2 == null)
|
||||
{
|
||||
audioSource2 = audioSourcePool.GetAudioSource("Music");
|
||||
audioSource2.clip = clip;
|
||||
audioSource2.volume = volume;
|
||||
currentAudioSource = audioSource2;
|
||||
currentAudioSource.Play();
|
||||
CoroutineHelper.StartCoroutine(FadeMusic(audioSource2, fadeDuration, audioSource1));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("已同时存在两个背景乐在切换");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭背景音乐
|
||||
/// </summary>
|
||||
/// <param name="fadeDuration">关闭时的自然过渡时间</param>
|
||||
public void Stop(float fadeDuration)
|
||||
{
|
||||
AudioSource source = audioSourcePool.GetAudioSource("Music");
|
||||
if (source == null) return;
|
||||
|
||||
CoroutineHelper.StartCoroutine(FadeOutMusic(source, fadeDuration));
|
||||
CoroutineHelper.StartCoroutine(FadeOutMusic(currentAudioSource, fadeDuration));
|
||||
}
|
||||
|
||||
private IEnumerator FadeMusic(AudioSource source, AudioClip clip, float fadeDuration)
|
||||
/// <summary>
|
||||
/// 切换音频
|
||||
/// </summary>
|
||||
/// <param name="source1">播放的音频</param>
|
||||
/// <param name="fadeDuration">变化时间</param>
|
||||
/// <param name="source2">停止的音频</param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeMusic(AudioSource source1, float fadeDuration, AudioSource source2 = null)
|
||||
{
|
||||
yield return FadeOutMusic(source, fadeDuration);
|
||||
yield return FadeInMusic(source1, fadeDuration);
|
||||
|
||||
source.clip = clip;
|
||||
source.Play();
|
||||
|
||||
yield return FadeInMusic(source, fadeDuration);
|
||||
if(source2 != null)
|
||||
{
|
||||
yield return FadeOutMusic(source2, fadeDuration);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭音频的协程
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fadeDuration"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeOutMusic(AudioSource source, float fadeDuration)
|
||||
{
|
||||
float startVolume = source.volume;
|
||||
@@ -47,9 +93,24 @@ public class MusicPlayer
|
||||
}
|
||||
|
||||
source.Stop();
|
||||
source.volume = startVolume;
|
||||
audioSourcePool.ReturnAudioSource("Music", source.gameObject);
|
||||
|
||||
if(currentAudioSource == audioSource1)
|
||||
{
|
||||
audioSource2 = null;
|
||||
}
|
||||
else if(currentAudioSource == audioSource2)
|
||||
{
|
||||
audioSource1 = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开启音频的协程
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fadeDuration"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeInMusic(AudioSource source, float fadeDuration)
|
||||
{
|
||||
float targetVolume = source.volume;
|
||||
|
||||
Reference in New Issue
Block a user