如何在 Unity C# 中淡化音频

How to Fade Audio In Unity C#

我写了一个脚本来播放数组中的随机音频文件。但我不知道如何在文件末尾淡出当前播放的音频并淡入新的音频文件。下面是代码。我现在使用的音频文件是 mp3 文件,我打算切换到 wave 文件。 以及对此的任何建议

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
public class BackGroundAudio : MonoBehaviour
{
    public AudioSource audioSource;
    public AudioClip[] audioClipsArray;
    public String FileName;
    // Start is called before the first frame update

    void awake()
    {
       audioSource = GetComponent<AudioSource>(); 
       
    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (!audioSource.isPlaying)
         {
             PlayRandom();
         }
    }

    void PlayRandom()
    {
        audioSource.clip=audioClipsArray[Random.Range(0,audioClipsArray.Length)];
        audioSource.clip =trimSilence(audioSource.clip);
        audioSource.PlayOneShot(audioSource.clip);
    }
    AudioClip trimSilence(AudioClip inputAudio, float threshold = 0.05f)
    {
        // Copy samples from input audio to an array. AudioClip uses interleaved format so the length in samples is multiplied by channel count
        float[] samplesOriginal = new float[inputAudio.samples * inputAudio.channels];
        inputAudio.GetData(samplesOriginal, 0);
        // Find first and last sample (from any channel) that exceed the threshold
        int audioStart = Array.FindIndex(samplesOriginal, sample => sample > threshold),
            audioEnd = Array.FindLastIndex(samplesOriginal, sample => sample > threshold);
        // Copy trimmed audio data into another array
        float[] samplesTrimmed = new float[audioEnd - audioStart];
        Array.Copy(samplesOriginal, audioStart, samplesTrimmed, 0, samplesTrimmed.Length);
        // Create new AudioClip for trimmed audio data
        AudioClip trimmedAudio = AudioClip.Create(inputAudio.name, samplesTrimmed.Length / inputAudio.channels, inputAudio.channels, inputAudio.frequency, false);
        trimmedAudio.SetData(samplesTrimmed, 0);
        return trimmedAudio;
    }
}

像这样的东西应该可以做到,你可以用混音器做更好的解决方案,但我相信这是最低的 effort/easiest 实现。

换句话说:当您开始剪辑时,它会在 FADE_TIME_SECONDS 秒内将 audioSource 的音量从 0 设置为 1。然后同时,我们启动一个延迟了剪辑长度减去 FADE_TIME_SECONDS 的协程并执行相同的操作,但是从 1 到 0,这为下一首曲目设置,从 0 开始音量并淡入。

在代码中:

const float FADE_TIME_SECONDS = 5;

void PlayRandom()
{
    StartCoroutine(FadeIn());

    audioSource.clip = audioClipsArray[Random.Range(0,audioClipsArray.Length)];
    audioSource.clip = trimSilence(audioSource.clip);
    audioSource.PlayOneShot(audioSource.clip);
    
    StartCoroutine(FadeOut(audioSource.clip.length - FADE_TIME_SECONDS));
}

IEnumerator FadeOut(float delay) 
{
    yield return new WaitForSeconds(delay);
    var timeElapsed = 0;

    while (audioSource.volume > 0) 
    {
        audioSource.volume = Mathf.Lerp(1, 0, timeElapsed / FADE_TIME_SECONDS);
        timeElapsed += Time.deltaTime;
        yield return;
    }
}

IEnumerator FadeIn() 
{
    var timeElapsed = 0;

    while (audioSource.volume < 1) 
    {
        audioSource.volume = Mathf.Lerp(0, 1, timeElapsed / FADE_TIME_SECONDS);
        timeElapsed += Time.deltaTime;
        yield return;
    }
}