Sound is played with a delay.

I have used “Best Latency” in the sound manager and still the sound has a very big delay while playing. Is there a solution for this?

Is it an audio sample, like an mp3 or wav etc?

@BrandyStarbrite I have tried several audio files(.mp3, .ogg, .wav) but doesn’t make any difference, same delay in every format. I am just using AudioSource.PlayOneShot(audio clip, nothing else. Is it Unity bug? I’m using latest Unity(2018) by the way.

1 Like

Okay, thanks for that info.
Oh crap! I forgot to ask this question:

Is the delay, at the start of each audio clip?

And, when you play the audio clips, in an external media player
like vlc media player etc, is there a delay?

@BrandyStarbrite
Is the delay, at the start of each audio clip? - Yes, same delay for all the clips I have tried.
And, when you play the audio clips, in an external media player
like vlc media player etc, is there a delay? - No, the delay is coming from Unity.

By the way I had to use this asset - Native Audio | Audio | Unity Asset Store - And now the problem is gone.

1 Like

Has anyone had any progress on this?

I get latency in Windows just running this test scene in the editor - not on android or anything - this is all the scene is, a single object with this code which plays a snare .wav file:

public class playSound : MonoBehaviour {

    public AudioClip prefabSound;
    AudioSource audioSource;

    // Use this for initialization
    void Start () {
        audioSource = GetComponent<AudioSource>();
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown("space")){
            audioSource.PlayOneShot(prefabSound, 1F);
        }
    }
}
  • Latency is set to “best”
  • Sounds are “decompress on load” and “preload audio data”
  • The .wav files do not have any baked delay in them, they start at 0.
  • If I get the getkeydown to do something visual there is no visual delay, so I don’t think it’s input delay.

Did you ever solve this problem?

I am facing this problem. please let me know your opinion.

we are facing the same issue…nobody has a solution on that yet ?

My guess would be that PlayOneShot() creates this delay because the sound has to be loaded into memory first. Is there still a delay if you set the clip of the audio source in the editor and just call Play() in the script?

That’s happen with Play() too.

Facing the same problem, please help me.

Does this delay correlate with the loading state of the clip? Perhaps try something like:

public class ExampleClass : MonoBehaviour
{
    public AudioClip clip;

    void Start()
    {
        AudioSource audioSource = GetComponent<AudioSource>();
        audioSource.PlayOneShot(clip);

        do {
            Debug.Log(clip.loadState);
        } while (clip.loadState != AudioDateLoadState.Loaded)
    }
}

or something like this (perhaps set the Debug.Log() line in the Update method). Check the console and see if the time until it’s loaded roughly equals the delay.

Also, how long is the delay? Are we talking of like 50ms, 500ms, 1sec or even longer?

A general tip to everyone who’s experiencing this issue is to ensure that you’re not testing over bluetooth audio.

48 Likes

I think this is very old problem and unity never ever try to fix it

1 Like

@Thaina Yes you are right. @Bosskee The delay is with direct sound and not with bluetooth.@Benergy the delay is more than 500ms.

1 Like

I don’t think that’s correct. Loading should be no different than other methods such as Play that execute on the same Audio Source. If its an audio clip with “preload audio data” checked (the default) and is not a resource file, addressable or compressed file, there should be no noticeable latency in the Unity editor. But on Android, everything has latency unless you use native code.

I thought I had this problem, but my Random Audio Script was cycling through a few options while my boolean was still true before playing the final audio choice. Just in case anyone else is using some sort of audio clip array and a boolean that is true too long.

And in the Audio Source Priorities I changed it to High.

I have some code I created for a typewriter effect. display a new character, make a typewriter click sound.
I notice some latency.
I came upon this thread, and as a result decided to measure the latency.
these are the different setups:

  • audio_dsp-buffer-size : best latency, audio clip type - ogg
  • audio_dsp-buffer-size : best latency, audio clip type - wav
  • audio_dsp-buffer-size : default, audio clip type - ogg
  • audio_dsp-buffer-size : default, audio clip type - wav

I know the audio clip length, and by subtracting it from the elapsed time (from the moment I call PlayOneShot() till the moment audio_source.isPlaying == false), I get the delay unity introduces.

The build settings are for windows, and I use the internal-IDE Game window, direct sound.
I averaged between 20 and 40 playbacks, for each setup.

Results

  • audio_dsp-buffer-size : best latency, audio clip type - ogg - 11 msec
  • audio_dsp-buffer-size : best latency, audio clip type - wav - 11 msec
  • audio_dsp-buffer-size : default, audio clip type - ogg - 14 msec
  • audio_dsp-buffer-size : default, audio clip type - wav - 14 msec

Code

    void Update()
    {
        int cur_story_length = storyText.text.Length;
        if (cur_story_length < story_level_1.Length)
        {
            if (audio_source.isPlaying == false && has_audio_started)
            {
                has_audio_started = false;
                Debug.Log("audio stopped. " + cur_audio_clip + " delay " + (Time.time - time_since_last_char - cur_audio_clip.length));
            }
            float cur_time = Time.time;
            if (cur_time - time_since_last_char > random_interval)
            {
                char c = story_level_1.ToCharArray()[cur_story_length];
                time_since_last_char = Time.time;
                switch (c)
                {
                    case '.':
                        cur_audio_clip = bellSFX_ogg;
                        break;
                    default:
                        if (cur_story_length % 2 == 0)
                            cur_audio_clip = clickSFX_wav;
                        else
                            cur_audio_clip = clickSFX_ogg;
                        break;
                }
                Debug.Log(cur_time + " " + cur_audio_clip + " random_inerval = " + random_interval);
                has_audio_started = true;
                audio_source.PlayOneShot(cur_audio_clip);
                storyText.text += c;

                // Select next interval
                Random.InitState((int)Time.time * 1000);
                random_interval = Random.Range(0.5F, 1.5F) * time_base_interval_between_characters;
            }
        }
    }

Maybe this is platform specific? I will try to have the logs output directly on a windows console, and on an android platform via logcat, and see the differences.

Update
building the windows app and running it with logs directed to a file, I get these worse delays, with the audio-dsp set to “best latency”:

ogg : 38 msec
wav : 41 msec

I have to say that calling AudioSource.isPlaying has a significant impact on audio performance, and I had to dilute the number of times I call it. Otherwise, the audio becomes jittery and distorted. I diluted the calls by checking isPlaying() only after AudioSource.Length has elapsed.

Here is the updated code

    void Update()
    {
        int cur_story_length = storyText.text.Length;
        if (cur_story_length < story_level_1.Length)
        {
            float cur_time = Time.time;
            if (cur_audio_clip != null &&
                (cur_time - time_since_last_char > cur_audio_clip.length) &&
                (audio_source.isPlaying == false && has_audio_started))
            {
                has_audio_started = false;
                Debug.Log("audio stopped. " + cur_audio_clip + " delay " + (Time.time - time_since_last_char - cur_audio_clip.length));
            }
            if (cur_time - time_since_last_char > random_interval)
            {
                char c = story_level_1.ToCharArray()[cur_story_length];
                time_since_last_char = Time.time;
                switch (c)
                {
                    case '.':
                        cur_audio_clip = bellSFX_ogg;
                        break;
                    default:
                        if (cur_story_length % 2 == 0)
                            cur_audio_clip = clickSFX_wav;
                        else
                            cur_audio_clip = clickSFX_ogg;
                        break;
                }
                Debug.Log(cur_time + " " + cur_audio_clip + " random_inerval = " + random_interval);
                has_audio_started = true;
                audio_source.PlayOneShot(cur_audio_clip);
                storyText.text += c;

                // Select next interval
                Random.InitState((int)Time.time * 1000);
                random_interval = Random.Range(0.5F, 1.5F) * time_base_interval_between_characters;
            }
        }
    }

Update 2

On Android, similar delay (33 msec and 50 msec observed, similar average to windows - ~40 msec).
However, the video event seems a lot closer, on the time line, to the audio event, in Android.
In windows, the video event seems to precede the audio event by a greater interval.

Update 3
On Android via BT Audio, I get similar numbers (40 msec).
However, I see that this measurement does not reflect reality, because a significant delay is apparent when observing the time difference between the visual and the audio, clearly several 100ths of milliseconds.
So, polling AudioSource.isPlaying proved to measure the time it took AudioSource to delegate the audio clip to the next block, i.e. a lower level audio driver (direct sound) or block (BT).
The additional delay caused by the low level driver or HW, or by the next communication channel (bluetooth), is not reflected by the state returned by isPlaying().

Hopefully more experienced game and audio programmers can note on ways the delay can be more accurately measured.
Maybe

4 Likes

I’ve run into the same audio delay issue and have fixed it.
Two issues combined created noticeable lag inside the Editor.

  • switched from Bluetooth headset to an analog headset
  • changed “DSP Buffer Size” from “Default” to “Best latency” (Project Settings > Audio)

Each of these two points otherwise create a small delay which adds up. Now I’ve got no lag at all.

4 Likes