- Home /
Audio Clip Getting Cut Short
I'm having problems with a few audio clips not playing correctly. All my clips are wav and generally less than 5 seconds long.
My sourcecode is really simple.
#pragma strict
var bonusSound : AudioClip;
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Player") { // See if the impact was the player
if(bonusSound) // Check if there is a sound clip attached to this bonus
AudioSource.PlayClipAtPoint(bonusSound, transform.position, 0.1 ); // If there is, play it
}
}
This is attached to my "bonus" gameObject, which is a prefab and is located twice throughout the level. The first time it plays the audio for the first bonus, it usually works ok. Sometimes it gets cut off half way through. The second bonus almost never plays correctly. It plays for a fraction of a second (maybe 0.1 seconds), sometimes nothing at all.
I should note that I've tried both 2D and 3D audio for this clip.
I think it may be related to the number of sound files I have running at one time as when I remove some enemies the problem is much less likely to occur. There is about 80-130 audio clips running most of the time (and when the problem occurs), but there are areas of my level running about 170 audio clips with no problem.
This is on a PC, not a mobile so I would imagine that certainly it should be able to handle a lot more than that.
I should point out that I've also tried just using "audio.Play" and attaching the audio clip directly, but that has the same problem. My audio file itself is definitely ok as I've been using it for months now.
Is there perhaps a better way that I can be playing the sound files?
Answer by aldonaletto · Jul 11, 2013 at 01:07 AM
It seems that you're playing too much sounds at the same time. If this is the case, maybe handling the property AudioSource.priority could solve your problem. Unfortunately, you can't set the priority of sounds played by PlayClipAtPoint because it doesn't give any reference to the temporary AudioSource created (which has default priority, 128). You could try to set a lower priority (129..256) in the less important sources, or instead create your own PlayClipAtPoint with a higher priority (0..127):
function PlayClipAtPoint(clip: AudioClip, pos: Vector3, vol: float){
var temp: GameObject = new GameObject("TempAudio");
temp.transform.position = pos;
var audioS = temp.AddComponent(AudioSource);
audioS.clip = clip;
audioS.volume = vol;
audioS.priority = 10; // define a high priority
audioS.Play();
Destroy(temp, clip.length);
}
NOTE: Code based on the original PlayClipAtPoint.
Thanks, aldonaletto. I'm a little worried about creating new gameObjects and adding components to them on the fly (for performance), but I'll give this a try.
So with the priority setting, do you know if it cuts off sounds that go beyond what it can play, or will it reduce their bit rate and still play them?
It is indeed a lot of sounds - there's a LOT of weapons in this scene :)
No, I don't know exactly what Unity does when the limit is reached - the docs say that the less important sounds are virtualized, but I don't have the slightest idea of what this means!
Don't be afraid of this version of PlayClipAtPoint: Unity's version is exactly the same - the only difference is the setting of priority, which doesn't exist in the original code.
Thanks again, aldonaletto.
Since this is creating a new gameObject with an AudioSource attached, I may as well add the AudioSource onto the existing object. However, I was still having problems with audio being cut even when doing it that way. But your priorities suggestion should help me to fix the problem. So thanks! :)
Your answer
Follow this Question
Related Questions
Audio, GetData not returning Data. 0 Answers
Intensity of sound. -1 Answers
Audio Sounds Blurring Together 0 Answers