- Home /
How to change priority of audio clips
Hi,
im creating a scene with 128 recorded ambient sounds. Each audio source for a different position in the scene. But I´m exceeding the play limit of 32 simultaneous audio files. It does´n work to use the collide function for play the sounds because the ambient sounds must be played simultaneously to stay synchronized. Can I write a script to change the priority of each audio clip while collide to each game object?
Answer by aldonaletto · Nov 08, 2013 at 12:36 AM
If these sounds are 3D, I suspect that you don't have to worry about this: according to the docs, sounds that are less audible (the farthest ones) are occluded first when there are more AudioSources than available mixer inputs (priority is also taken into account).
Anyway, I suppose that you could use a trigger to raise/lower the priority of each sound source: add or child a sphere trigger of the desired radius (range) to the player, and add also a kinematic rigidbody to it (moving triggers require a rigidbody, even kinematic), then place this code in every AudioSource:
function OnTriggerEnter(other: Collider){
// audio source inside player range:
audio.priority = 100; // set a higher priority
}
function OnTriggerExit(other: Collider){
// audio source out of player range:
audio.priority = 128; // restore default priority
}
NOTE: Every object having one of these AudioSources must also have a collider in order to be detected by the trigger (rigidbody not required).
Thank you, it was exactly what I was looking for. But it still not working. I find out that there are only 100 sound sources playing at the same time and in the scene are 160 sound sources.
When I actived "play on awake" for all the audio sources, some random sources stay inactive. If I only activate "play on awake" to a reduced amount of audio sources the selected sources play with out problems but the rest (unselected sources) don´t can be heard. Is it possible to change the "play on awake" function after the game is started?
You'll only have so many sound channels available. The idea of priority is to ensure that the sounds you want to be heard are heard at the expense of the low priority sounds not being played.
Answer by chaguaramo · Apr 04, 2014 at 01:52 AM
Hi, with this script I can create audio zones with a random form that are on while the rest of my audio sources are turned off. So I could reduced the numbers of audio sources playing simultaneously. Thanks to Daniel Murcia!
For use it, create a surface with the form you need and place it on the floor level, add a mesh collider, select-on the is trigger option, unselect the mesh render option and attach to the surface you have created the audio sources you want to be played simultaneously when enter in the zone designed by your surface.
So you can differentiate audio zones that are played at once when they are triggered.
#pragma strict
var audioSources;
function Start(){
audioSources = gameObject.GetComponentsInChildren(AudioSource);
}
function OnTriggerExit(){
Debug.Log("stopping sounds");
Debug.Log(this);
var self = this;
for (var source:AudioSource in audioSources) {
Debug.Log(source);
source.Stop();
}
}
function OnTriggerEnter(){
Debug.Log("starting sounds");
Debug.Log(this);
var self = this;
for (var source:AudioSource in audioSources) {
Debug.Log(source);
source.Play();
}
}