Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio plays correctly in editor, but slowed down in game

Discussion in 'Audio & Video' started by calebc01, Jul 24, 2016.

  1. calebc01

    calebc01

    Joined:
    Jul 21, 2015
    Posts:
    62
    I'm using an AudioSource component to play audio clips on a character in my game. All clips play correctly from the editor, but some of them are slowed down when played in-game. It happens on a per-clip basis, without changing AudioSource settings.

    Does anyone have an idea why that might happen? The code I use to play the clips is shown below:

    Code (csharp):
    1.  
    2. public class AudioHelper : MonoBehaviour
    3. {
    4.     public AudioSource[] _audioSources;
    5.    
    6.  
    7.     public void PlayClip(AudioClip clip, float volume)
    8.     {
    9.         AudioSource source = GetFirstAvailableAudio();
    10.  
    11.         if (source == null)
    12.         {
    13.             return;
    14.         }
    15.  
    16.         source.clip = clip;
    17.         source.volume = volume;
    18.         source.Play();
    19.     }
    20.  
    21.     // Grab an available audio source
    22.     private AudioSource GetFirstAvailableAudio()
    23.     {
    24.         for (int index = 0; index < _audioSources.Length; index++)
    25.         {
    26.             if (!_audioSources[index].isPlaying)
    27.             {
    28.                 return _audioSources[index];
    29.             }
    30.         }
    31.  
    32.         return null;
    33.     }
    34. }
    35.  
    There are multiple audio sources on my object, so that I can play more than one sound at a time. But they are all identically configured, and a specific set of audio clips always play slowly, regardless of which source they are playing on.

    Thanks!

    EDIT: THE PLOT THICKENS!

    OK, so the changed playback speed has nothing to do with the clip, and everything to do with *where and when it is played. In my character script, when it is first created, it makes a noise indicating that it has spawned, like so:

    Code (csharp):
    1.  
    2.     public override void AfterSpawn()
    3.     {
    4.         _audioHelper.PlayClip(GetRandClipFromArray(_playerAwareClips), 1.0f);
    5.     }
    6.  
    I can play any of the clips there without problems. However, later, when the character attacks the player, my PlayClip function is called from within the Update() function, like so:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.      // Bla bla bla, irrelevent stuff
    5.  
    6.      // Attack again if it has been long enough
    7.      if (Time.time - _lastAttackTime >= _attackPeriod)
    8.       {
    9.              _audioHelper.PlayClip(GetRandClipFromArray(_attackClips), 1.0f);
    10.              _lastAttackTime = Time.time;
    11.              _anim.SetTrigger("Attack1");
    12.       }
    13. }
    14.  
    Any audio clip played from within the Update() function is oddly slowed down! I never change the playback speed, so... not sure what is going on here.
     
    Last edited: Jul 24, 2016
  2. calebc01

    calebc01

    Joined:
    Jul 21, 2015
    Posts:
    62
    teel1ng, ZBR_01 and gareth_withVR like this.
  3. oigetinthevan

    oigetinthevan

    Joined:
    Aug 1, 2018
    Posts:
    6
    Just adding a 2021 solution to this issue. The pitch of my audio source was set to 0.1 instead of 1. Slid that bad boy up to 1 and voila - problem solved.
     
    ZBR_01 likes this.