I can't solve the audio lag problem, I tried all I could.

Hi,

I tried everything, and I can’t seem to figure out the problem with why is my audio lagging.

The audio needs to be played in the same moment when the player destroys a particular object.
Here is the code that does this:

    void OnTriggerEnter2D(Collider2D col)
            {
               string tag = col.gameObject.tag;
               //this plays the audio, will get into it as well
               AudioManager.Instance.SoundFx(tag);
               //This destroys the gameObject, so SoundFX() is called before it
               OnHitResponse(col.gameObject);
             }

Now, lets take a look inside AudioManager.SoundFx method:

public void SoundFx(string tag)
        {
            switch(tag)
            {
                case "Normal":
                case "Hard":
                case "Transparent":
                    LadderPlay(hitsQTY, tag);
                    //I am recording hits Quantity to know which note to play
                    hitsQTY++;
                    break;
                case "NotDisappear":
                    PlayOneShot(notDisappear);
                    break;
                case "PortalA":
                case "PortalB":
                    PlayOneShot(portal);
                    break;
                case "Death":
                    PlayOneShot(death);
                    break;
            }
        }

And here is the LadderPlay method, part of AudioManager class:

public void LadderPlay(int k, string tag)
        {
            // Both triangle and harp sounds will be played with ladder effect
            string type = (tag.Equals("Transparent")) ? "Triangle" : "Harp";
            // Directory that holds the Triangle and Harp sound effects
            AudioClip[] currentDict = audioDict[type];
            if (PlayerPrefsX.GetBool(Constants.PrefsIsSoundOn))
            {
                if (k > currentDict.Length - 1)
                    k = currentDict.Length - 1;
                audioSource.PlayOneShot(currentDict[k], sfxVolume);
            }
        }

That’s the whole code that is called when the audio needs to be played.

Now please, let me tell you all I’ve tried so far:

  1. I used profiler to determine that ‘OnHitResponse’ (called after the AudioManager.SoundFX() method), is making my game fall to 30 FPS. I fixed that, without fixing the audio lag (but hey, my game now runs smoothly on 100fps, no matter when).
  2. I noticed I have .mp3 files, so I changed that immediately, re-creating the sounds in .wav format.
  3. I changed the AudioSettings lag settings to - Best Latency (this made my sound FX to have some white noises while playing them, and it seemed that the lag has gone. But when I tried the game on my Android, the lag returned, and the white noise DISAPPEARED.
  4. I triple checked that the sound waves in the FX files start from t0 (first second).

Any ideas guys and gals?

I am stuck on this for a week, and I fear that the solution will be something really simple.
Thanks a lot for your time, it will save mine significantly. :slight_smile:

And have you set to preload audio data and decompress on load? Unity - Manual: Audio Clip

Try my tips from this question AudioSource.Play lags.