Search Unity

Audio clicking sound when switching between audiosources?

Discussion in 'Audio & Video' started by Jeepster, Jan 2, 2020.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    I've been trying to fix the clicking sound that occurs when I abruptly switch between audiosources with a coroutine to fade out the sound:
    Code (CSharp):
    1.     IEnumerator VolumeFade(AudioSource idleUW, float _EndVolume, float _FadeLength)
    2.     {
    3.         float _StartTime = Time.time;
    4.         while (!idleUW &&
    5.              Time.time < _StartTime + _FadeLength)
    6.         {
    7.             float alpha = (_StartTime + _FadeLength - Time.time) / _FadeLength;
    8.             // use the square here so that we fade faster and without popping
    9.             alpha = alpha * alpha;
    10.             idleUW.volume = alpha * .3f + _EndVolume * (1.0f - alpha);
    11.  
    12.             yield return null;
    13.  
    14.         }
    15.  
    16.         if (_EndVolume == 0) { idleUW.UnPause(); }
    17.     }
    I then activate the coroutine :
    Code (CSharp):
    1.   if (rb.transform.position.y <= -1.45 && !Input.GetKey(KeyCode.W))
    2.         {
    3.                 StartCoroutine(VolumeFade(idleUW, 0f, 0.5f));
    4.  
    5.                 idleUW.enabled = true;
    6.        
    7.             speed1UW.enabled = false;
    8.             speed2UW.enabled = false;
    9.  
    10.         }

    But it doesn't stop the clicking. Does anybody know how to fix this?