audio.Play() not always playing the entire AudioClip

I'm getting very inconsistent results from audio.Play(), audio.PlayOneShot and AudioSource.PlayClipAtPoint. Some times the clip will play through correctly, while other times it will only play a portion and sometimes not at all.

Context: This is for a very simple minigame where the user has to touch targets as they move across the screen. A raycast figures out what they touched and calls a function on the target that plays a Maraca-shake sound, adjusts the score, instantiates an effect popup and at the end, destroys the GameObject. Everything else works perfectly except for the sound. When it doesn't work, it plays such a short portion of the clip that it only sounds like a "tick".

Sometimes I can go an entire play session without an occurrence and other times the problem happens 50% of the attempts, so I know it's not a problem with a specific object/prefab/inspector setting. They're all using the same script with the same settings.

I've tried all three methods of playing the sound listed above and all have the same problem. I've disabled the Destroy command to see if the sound was getting cut off too early, but that didn't work either. I'm not sure what else to try at this point. This is in Unity iPhone 1.7 basic. The error occurs both in the editor and on the device(iPad and iPhone).

If you aren't destroying the gameObject that holds the audio source (my first though), then it could be any of the following.

  1. You're using MP3s (someone else mentioned this).
  2. You're using Play, rather than PlayOneShot(audioClipName). Play one shot can be triggered many times at once, and it'll deal with it. Play can not.
  3. Can you confirm with a line of debug code that you aren't accidentally triggering it 30 times? That would also cause issues.
  4. Are you doing anything to audio anywhere else, such as having an audio listener on the wrong camera, etc?

It may be that the audio buffer becomes full causing an error and cancelling the operation.

In other words the iphone can only play one compressed audio clip at a time, and all sound effects must be uncompressed (AIFF) audio to play simultaneously. The compressed audio is usually used for the background music as it would be larger.

If you try to play an additional audio clip that is compressed the audio processor will have insufficient memory bandwidth or capacity to fulfil the requested operation. Try to limit the number of uncompressed audio clips playing simultaneously to prevent a buffer overflow, and limit compressed audio to one clip only.

I am having the same problems in an ABC learning game. When the user clicks a button the sound of the letter is supposed to play. It works on most of the letters but not all of them. By changing the bitrate on one of the non-working letters, I was able to get it to work but this “fix” is not consistent. All of the sound files are mp3 and targeted for android devices but I test with PC. Please help.

I’ve only tested on the pc. I was able to solve the issue by rerecording the faulty sounds as wav. I’d prefer mp3 for filesize but the 6 letters as wav are doable. I noticed in other posts that it seems that mp3 files are a bit buggy. Is this true?

(2015)

If you are still having this problem and are using:

GetComponent().Play;

I managed to fix it, because before my gun would shoot it’s first bullet and sound weird, and then back to normal, but after 30 seconds, it would go weird again.

What you need to do is where your code is that says:

GetComponent().Play;

You need to have this underneath:

GetComponent ().Play (49); - This is mine anyway. The 49 refers as khz, meaning a delay as well.

So 44100 will give you a delay of exactly 1 second.

I basically divided 44100 by 90 and that has allowed me to have my gun sounds to work as normal.

I hope this helps.