Search Unity

Audio Problem with Audio Crackling

Discussion in 'Audio & Video' started by Narkata55, Jul 7, 2017.

  1. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Hey guys, was trying to figure out how to solve this issue on my own but to no avail. Basically, im looping a short .wav file for a sound effect when a button is pressed in my project, but for some reason, no matter what I do, there is an annoying crackle sound whenever the same clip is played quickly in succession. Ideally, I want it to just cut off the previous clip and play the sound again, which is why I put audioclip.stop() into my code, but it's not working. I tried adjusting the actual sound file in my DAW by adding some empty space at the beginning and end, but even then the crackle still happens, just without the sound effect even playing! Thanks for any advice you may have, and if you want to see some code or audio files of what's happening to make sense of this, please let me know and ill post it!
     
    Simba254 likes this.
  2. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    That's what's causing the popping. You are stopping the audio abruptly.

    Set the volume to zero. Stop the Audio. Reset the volume. Play the audio again.
    Maybe you get lucky and that works.

    If that doesn't work, you'll need to fade out the volume instead.

    You need a pool of audiosources. An array or list.
    How many audiosources you will need will depend on your specific case (how quickly you can fade the audio gracefully and how quickly the player is allowed to click the button).

    When the user clicks the button:
    -Loop through the audiosources and find an audiosource that isn't playing audio.
    -Play the audio with that audiosource.

    When they click the button again:
    Start a co-routine to quickly lerp the volume down on the previous audiosource (if it's still playing) and when the co-routine has reached volume zero, THEN stop that audiosource from playing.
    Reset the volume to its default.
    It is now ready to be used to play audio again.
    Aslong as it's not ready to play, you'll need to use another audiosource to play your audio.
    That's why you need multiple audiosources.
     
    Andrei2789, servetg and FlightOfOne like this.
  4. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Wow, thanks for the feedback! I guess I was wrong in thinking that I could just have an effects audiosource and a music audiosource. So having many more audiosources wont have too much of a negative effect on the project? Ill try adding a couple and looping through like you suggested
     
  5. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Thanks again! I added a bunch of audio source and with some loops, everything sounds fantastic! I appreciate the help, have a great night!
     
  6. CraigGraff

    CraigGraff

    Joined:
    May 7, 2013
    Posts:
    44
    Way late on this one, but this came up in my search and it turns out AudioSource.PlayOneShot works well for this.
     
    Magixxar, Earthshine and unnanego like this.
  7. Magixxar

    Magixxar

    Joined:
    May 15, 2016
    Posts:
    2
    Thanks dude, this works like a charm
     
  8. hid_

    hid_

    Joined:
    Apr 5, 2021
    Posts:
    10
    The thread is outdated but if for someone the problem is still actual.

    In my case crackling was randomly appear during 3 minute long mp3 audio playing in 2d UI.
    Configuration aduio asset didn't help. After some time of investigation I figured out that the root cause in particles (it was used as background for canvas) .

    By the way the issue is still wasn't fixed even in 2023.2 : https://issuetracker.unity3d.com/is...and-the-audio-playable-asset-has-loop-enabled
     
  9. enderandpeter

    enderandpeter

    Joined:
    Sep 16, 2015
    Posts:
    7
    This issue is a bit different for everyone, it seems. I had this problem with just a single WAV file. Every other file sounded fine, but this one WAV file always had a snapping at the end. This only happened when Unity played the audio file. The original audio file had no such oddity.

    I converted this WAV file to an MP3 and the snap at the end went away... strange but true.