- Home /
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).
Just to make sure, the game objects that you destroy, they don't have the sound clip, do they?
Nope, they just have a script that references the AudioClip. I get much better results with PlayClipAtPoint due to how it works compared to the others, but it still glitches at least once or twice in a level.
i'm having the same problem, but in a web build. ~$$anonymous$$att
Answer by jtbentley · Aug 29, 2010 at 02:59 AM
If you aren't destroying the gameObject that holds the audio source (my first though), then it could be any of the following.
- You're using MP3s (someone else mentioned this).
- 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.
- Can you confirm with a line of debug code that you aren't accidentally triggering it 30 times? That would also cause issues.
- Are you doing anything to audio anywhere else, such as having an audio listener on the wrong camera, etc?
Answer by BurningThumb · Aug 26, 2010 at 04:48 AM
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.
10 years later and this comment pretty much saved my project, thank you! While I'm building for PC instead of phone, it tipped me off to the idea that compressed audio has a limit within the engine/platform--never encountered that before. For me, I had a lot of sources in my scene using the same clip. Even though they were small and out of range most of the time, they prevented my ambient audio from playing properly. I noticed when I switched my ambient audio to Decompress on Load, the single clip would play fine. But this was not ideal, since I wanted to keep the compression for these long clips. So the solution was to change the out of range audio source clip to Decompress on Load instead of Compressed in $$anonymous$$emory. This gave enough room for the ambient audio to run as Compressed, even though as I mentioned you couldn't even hear the other sources. So ironically, making the clip uncompressed actually helped the audio engine/buffer to leave room for the compressed audio, which in turn allowed them to play properly. What's sad is that none of this gave any warnings or errors in Unity, which is a little scary. The only reason I knew it was happening was because I checked the audio mode in the scene view.
Answer by s1niz7el2 · Nov 20, 2011 at 10:12 AM
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.
Are you having this problem in the PC or only when testing in the Android?
Answer by s1niz7el2 · Nov 21, 2011 at 12:04 PM
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?
Answer by DimitriUK · Jul 17, 2015 at 07:01 PM
(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.