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

Weird Sound Looping When Playing Audio Track

Discussion in 'Audio & Video' started by Hotpots, Apr 24, 2016.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    So I am using mostly 3D sound within this application, the nature of the application is based on an adaptive environment. When the user interacts with an object it specifically changes the environment around them and also differing sound effects occur.

    However I'm having a problem that when playing an audio track in either 2D or 3D, the sound constantly sounds like its looping the first 1 second of the audio clip and sounds VERY jittery.

    Does anyone know the cause of such a problem?

    Thanks in advance :)
     
  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Problem sort of solved...

    Had the audio track being played in the update function, therefore it was continuously starting.
    However I have an IF condition in the update function and if it's true play the audiosource, how would I counter the problem of it constantly looping other than using a boolean?.
     
  3. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    Maybe check if it isn't already playing, you get an AudioSource.isPlaying field. So:

    Code (csharp):
    1.  
    2. if( shouldPlaySound && !source.isPlaying ) source.Play();
    3.  
     
  4. jrsimmonds

    jrsimmonds

    Joined:
    Sep 8, 2014
    Posts:
    1
    Can you put up the code you are using? I have had this issue before, its not looping the sounds it is just triggering the sound every second or so. or you using a PlayOneShot?
     
  5. Mengxiao_Yu

    Mengxiao_Yu

    Joined:
    Oct 25, 2018
    Posts:
    3
    I have exactly the same problem and I have solved this problem by adding a bool to tell whether the audio is already being played. When the audio is played, the value of the bool changes so the function will no longer be triggered.

    Code (CSharp):
    1. private bool TimeUpAudioIsPlaying;
    2.  
    3. void Start ()
    4.     {
    5.        TimeUpAudioIsPlaying = false;
    6.     }
    7.  
    8. void PlayTimeUpAudio()
    9.     {
    10.         if (gameOver == true && TimeUpAudioIsPlaying == false)
    11.         {
    12.             TimeUpAudio.Play();
    13.  
    14.             TimeUpAudioIsPlaying = true;
    15.         }
    16.     }
     
  6. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    Don't do too much in the update void. I suggest using temporary objects that make use of ienumerator and StartCoroutine. Then deleting that object out of the memory.