- Home /
Is it possible to change Audio Manager Volume by script?
Hi!
It happens that I have two audio Listeners in my scene (with one enabled when the other is disabled). I found out that no matter which Listener is enabled, if I would change the value in Audio Manager Settings (project settings, it will affect the overall volume. Well, is it possible to change that by script?
I know that I could achieve the same result by getting all the listener components (or the one currently enabled) and change their volume value. But except of that would be slightly more complicated, isn't it more efficient to approach it from the shortcut way I look for?
Thank you!
I was afterwards looking to what is called singleton pattern. I guess, it would be useful to understand the concept and it seems that it would do the job. But is there a quicker solution however?
Thinking back, I will just try to affect Object1.AudioListener.volume and Object2.AudioListener.volume from the script which controls when the overall, lets call it master, volume has to change. Probably I shouldn't post before I try some workarounds. But except of that I was looking for a quick, yes/no, answer, this last idea of $$anonymous$$e wouldn't be feasible for a big number of objects with audio listeners or if it was not known which instances would have audio listeners, I guess.
Answer by SrBilyon · Apr 06, 2013 at 08:42 PM
I'm not sure why you have two audio listeners.
Anyway, you could use AudioListener.volume to control the volume of the game (assuming one audio listener is used).
Or (untested), you would get all the audio listeners, loop through them, and modify the AudioListener.volume for each.
Thank you SirVictory for opening my eyes (or my ears in this case)!
First, the reason I used two audio Listeners, is just because of the simplest trick that came first on my $$anonymous$$d, when I wanted to use some different behavior of sound when I'm into pause mode. Although I cannot recall at the moment the details that made me to decide (and probably it was an unnecessary decision) towards this solution, however what I can now tell is that I used 1 audio listener too far away from the actual scene, in order to activate it when I pause the game. in other words, when I press pause, what I probably tried, was to not mess with taking care of how to mute the 3D audio clips, which are playing during the action. Ins$$anonymous$$d, I let them play and I used a non-3D audio clip, which is able to be listened only from that distant audio listener, as a background music. As I said, it might have been an unorthodox way.
About your second suggestion, I agree, that this was the first idea which came on my $$anonymous$$d, but I was wondering, if there is a direct way to change, by script, the Volume in Project Settings -> Audio, at the same fashion you can do with the default speaker mode for example.
With your first suggestion though, I realized how silly I am, for thinking that a component with this class has to be attached on every object with an audio listener and by being prejudiced from that, I didn't even read the script reference! Also, for some dark reason, while I was searching for "master volume" keywords and so on, I hadn't come across to the existing answer here.
By the way, what I noticed finally is that AudioListener.volume overrides the Audio manager settings. If the first is 1.0 and the latter 0.0, the audio will be listened. And since the post started with the volume in Audio $$anonymous$$anager, I see it takes values higher than 1 (also negative ones). Well, as I said, it takes no effect when a AudioListener.volume class comes in play, but, otherwise, it only affects the sound for values between 0 and 1. Why then the field 'Volume' accepts these other values? Is it just a little neglect? Finally any connection with the Audio $$anonymous$$anager volume parameter and the AudioListener.volume?