Search Unity

I can't get my audio to be lagless. Tried everything I could

Discussion in 'Audio & Video' started by Batka, Nov 22, 2016.

  1. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    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:


    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2.         {
    3.            string tag = col.gameObject.tag;
    4.            //this plays the audio, will get into it as well
    5.            AudioManager.Instance.SoundFx(tag);
    6.            //This destroys the gameObject, so SoundFX() is called before it
    7.            OnHitResponse(col.gameObject);
    8.          }
    Now, lets take a look inside AudioManager.SoundFx method:

    Code (CSharp):
    1. public void SoundFx(string tag)
    2.         {
    3.             switch(tag)
    4.             {
    5.                 case "Normal":
    6.                 case "Hard":
    7.                 case "Transparent":
    8.                     LadderPlay(hitsQTY, tag);
    9.                     //I am recording hits Quantity to know which note to play
    10.                     hitsQTY++;
    11.                     break;
    12.                 case "NotDisappear":
    13.                     PlayOneShot(notDisappear);
    14.                     break;
    15.                 case "PortalA":
    16.                 case "PortalB":
    17.                     PlayOneShot(portal);
    18.                     break;
    19.                 case "Death":
    20.                     PlayOneShot(death);
    21.                     break;
    22.             }
    23.         }
    And here is the LadderPlay method, part of AudioManager class:

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


    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. :)
     
  2. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    I did it!!!!

    AudioSource.PlayOneShot was causing the problem, it's a less efficient way to play audio.
    So instead of:

    Code (CSharp):
    1. audioSource.PlayOneShot(wantedClip);
    I did this:
    Code (CSharp):
    1. audioSource.clip = wantedClip;
    2. audioSource.Play();