- Home /
How to Stop all audio
I have a scene with about 20 game objects and when one is clicked, it begins playing audio. When the next on is clicked I need to stop any audio that's playing and play the next audio. Everything I've found regarding this topic says to set Time.timeScale to 0, but all that's going to do is pause everything. The other answer I keep seeing is that I should set AudioListener.volume to 0, which will be essentially muting everything.
Here's what I currently have so far. I just need to insert something to stop all audio before a new track begins. Sorry if this is a simple request, I couldn't find an answer anywhere!
function Update(){
if (audio.isPlaying){
transform.Rotate(.5, .25, .75);
}
}
function OnMouseDown(){
if (audio.isPlaying){
audio.Stop();
transform.rotation = Quaternion.identity;
}
else {
//Stop All Audio
audio.Play();
}
}
Answer by imnickb · May 07, 2012 at 01:30 PM
If anyone runs across this thread looking for an answer, here's how I ended up doing it. This is from Mortis on the Unity Forums.
private var allAudioSources : AudioSource[];
function Awake() {
allAudioSources = FindObjectsOfType(AudioSource) as AudioSource[];
}
function StopAllAudio() {
for(var audioS : AudioSource in allAudioSources) {
audioS.Stop();
}
}
Then just call the StopAllAudio() function when you want everything to stop.
Feedback from the OP is always welcome. $$anonymous$$ost time people misuse the answer function to post more questions or comments. So this gives me a little bit of hope that not the whole world run out of common sense ;)
+1
Life saver! Thanks OP (re$$anonymous$$ds me of Reddit somehow).
Ins$$anonymous$$d of initializing allAudioSources in Awake, I would recommend to use some local array variable inside StopAllAudio() method. This will be helpful in the case somebody is assigning component at runtime.
Answer by dpanov76mail-ru · Feb 25, 2015 at 10:37 AM
C# Version
//Stop all sounds
private AudioSource[] allAudioSources;
void StopAllAudio() {
allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach( AudioSource audioS in allAudioSources) {
audioS.Stop();
}
}
i get this error
Assets/Script/Playsound.cs(21,73): error CS0039: Cannot convert type UnityEngine.Object' to
UnityEngine.AudioSource[]' via a built-in conversion
Answer by JmPrsh153 · Jul 01, 2013 at 03:13 PM
http://docs.unity3d.com/Documentation/ScriptReference/AudioListener-pause.html
enough said ;)
function OnMouseDown(){
if(!paused)
{
Time.timeScale = 0;
paused=true;
Guiscript.enabled = true;
AudioListener.pause = true;
}
else
{
Time.timeScale = 3;
paused=false;
Guiscript.enabled = false;
AudioListener.pause = false;
}
}
forget the guiscript bit thats just my way of showing my gui when i press the pause button but this method works :)
Take in account that with Time.timeScale = 3
you passing the time x3 times faster, normally you want to be Time.timeScale = 1
Answer by Nerull22 · Dec 11, 2011 at 05:11 PM
Well what you could do, is put all your audio sources into an array. And then when you want to stop all audio, just cycle through the array with a for loop stopping all the audio sources and then start playing the new audio source.
None of this code is tested, so just use it as a reference.
private AudioSource arrAudio;
Start()
{
//Declare the array here and place your audio sources inside of it.
}
Update()
{
for(var int i=0; i < arrAudio.length; i++)
{
arrAudio[i].stop();
}
arrAudio[i].Play(); //This is to play the new audio source.
}
So try something like that, and see if it works for you.
The basic idea is good but your code is a bit messy. It's no language i know. I guess it should be C# (due to your variable declaration), but the rest...
Your methods always need a return type in C# (void in this case which means nothing is returned).
The for loop in Update makes no sense. It should be in the On$$anonymous$$ouseDown event when you want to stop the audio.
arrAudio should be an array so it have to be declared as array:
private AudioSource[] arrAudio;
or to match the OPs language (Unityscript):private var arrAudio : AudioSource[];
In your example "i" is only valid inside the for loop so using it outside won't work and doesn't make sense in this case.
Like I said, use it as a reference. I was just waking up when I wrote this. And I normally have reference next to me. So I apologize for the sloppy code, but I'm just hoping that the concept did get across.
$$anonymous$$y apologies.
Thanks for the input! I haven't gotten to look at this yet but I'm planning on it tomorrow. Does anyone else have any insight on this?
Answer by jister · Dec 24, 2011 at 01:28 AM
this turns them on and off still need to find some way to look if there is sound playing and stop that. sadly enough if(audio.isPlaying){audio.stop();} doesn't work :(
function OnMouseOver (){
if(GetComponentInChildren(AudioSource).enabled == false && Input.GetMouseButtonDown(0)){
GetComponentInChildren(AudioSource).enabled = true;
}
else if(GetComponentInChildren(AudioSource).enabled == true && Input.GetMouseButtonDown(0)){
GetComponentInChildren(AudioSource).enabled = false;
}
}