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();
      	}
}

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.

C# Version

//Stop all sounds
private AudioSource[] allAudioSources;

void StopAllAudio() {
	allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
	foreach( AudioSource audioS in allAudioSources) {
		audioS.Stop();
	}
}

enough said :wink:

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 :slight_smile:

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*.stop();*

}

arrAudio*.Play(); //This is to play the new audio source.*

}
So try something like that, and see if it works for you.

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 :frowning:

    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;
	
	}

}

in Unity 5.3 it’s now simply:

AudioListener.pause = !isEnabled;

C# Version - Adapted
From @dpanov76mail-ru

This worked for me, muting every source of audio, playing or not. This surprising works like a Pause function, resuming the audio when you like.

Simply call function with true to play audio, false to pause.

	private AudioSource[] allAudioSources;

void AudioControl(bool decision) {
	allAudioSources = FindObjectsOfType<AudioSource>() as AudioSource[];
	foreach( AudioSource audioS in allAudioSources) {
		audioS.mute = !decision;
	}
}

AudioListener.volume = 0;

Hey @imnickb,
I know it’s pretty late to answer on this topic.
And maybe the answer I provide has simply not available at the time you’ve been asking but how about just pausing your AudioListener:

Regards,Hey imnickb,
I know it’s pretty late to answer on this topic.
And maybe the answer I provide has simply not available at the time you’ve been asking but how about just pausing your AudioListener:

Regards

AudioSource allAudio; //Create A List

void Start() {
allAudio = FindObjectsOfType<AudioSource>(); //Find All AudioSources
for(int i =0; i<allAudio.Length;i++){
allAudio_.Stop(); //Stop All AudioSources Using The **for** loop_

}
}