- Home /
How can you stop all other audio sources from playing and then start a new audio source?
This should be really, really easy and I'm ripping my hair out over this. C# language.
My goal is to have two separate triggers used to play a different song when the player (and only the player) walks into them. For an example, imagine a building with 3 rooms; on the left is a room that plays the super mario brothers music and on the right there is a room that plays sonic the hedgehog music. Both rooms have their music activated only by the player when he walks into the room and it CONTINUES to play until another audio source is played, in which case it will stop and be replaced.
Unfortunately my code has it set up to where both songs play simultaneously. I can stop ALL music, but I can't stop one individual track and let another play in the same scene.
I have my music script on the player and have two separate objects being used as triggers when I walk through a doorway. I managed to stop them from repeatedly starting over, but no matter what I do I cannot get one trigger to stop the audio being played prior to it being activated.
I'm using an array to list two songs [0, 1] and can get both songs to play when my player moves onto the trigger, but cannot get them to stop so the next song plays on its own!
audio.Stop(); doesn't work, no matter where I put it or how many times I use it. I've poured over the scripting reference, all of Unity Answers, google, and have watched like 5 audio tutorials and no one really touches on this question.
Code below:
using UnityEngine;
using System.Collections;
public class MusicChange : MonoBehaviour {
public AudioClip[] Music;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if(gameObject.tag == "Testing")
{
if(audio.clip != Music[0])
{
audio.Stop();
if(other.gameObject.tag == "Player")
{
audio.clip = Music[0];
audio.Play();
}
}
}
if(gameObject.tag == "Overworld")
{
if(audio.clip != Music[1])
{
audio.Stop();
if(other.gameObject.tag == "Player")
{
audio.clip = Music[1];
audio.Play();
}
}
}
}
}
what is your audio source attached to, the character or the triggers?
Currently it is set to the player, but I have attempted it to be set on the player, triggers, camera, or a combination of them to no avail.
I can make it to where the sound stops, then immediately starts again (when inside the trigger) or make the song stop with OnTriggerExit, but it's not suited to my needs.
No matter what I seem to try, I can't make a song start when the player walks into collider A and then have that song stop and a different one start when the players walks into collider B.
Try getting audio in the start function and storing it
ie, private AudioSource myAudio;
then in start,
myAudio = audio;
then just use myAudio throughout..
Unfortunately it doesn't work this way either. :(
A few quirks I've noticed:
1) When my player walks through the FIRST trigger... nothing happens. When he walks into the second, the audio will play for just one blip and then cease.
2) When I changed "$$anonymous$$usic[0]" and "$$anonymous$$usic[1]" to just "audio" in general, I noticed for some reason that the audio didn't play for the first trigger, but played completely for the second!
3) The music that started playing for the 2nd trigger will stop when I enter into the first trigger or any other, including my camera bounds. I went ahead and turned off the camera and played from the scene view and confirmed.
4) I fiddled with the tags to try to figure out more; switched "Testing" / "Overworld" with Player and put it only on the Player. Worked the same as above.
Reading the code I feel it SHOULD be working, I don't know why it isn't! What in my code would cause ANY trigger to stop the audio?
Answer by LightSource · Jun 24, 2013 at 01:53 AM
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.
Original: http://answers.unity3d.com/questions/194110/how-to-stop-all-audio.html
I saw that when searching and tried it, but it was unsuccessful (also in Java as opposed to C#, but what can ya do XD).
Answer by evand · Jun 26, 2013 at 12:44 AM
I have solved this issue!
When you are trying to activate an individual audio source it AUTOMATICALLY STOPS THE CURRENTLY PLAYED SONG.
When I put in "audio.Stop" or an equivalent, the program's thought process was
" Trigger is right. Trigger song is right. Other Trigger is right. Stop current song and play new song. Stop song. "
void OnTriggerEnter(Collider other)
{
if(gameObject.tag == "Player")
{
if(myAudio.clip != Songs[0])
{
// audio.Stop();
// The above line is what needed to be removed.
if(other.gameObject.tag == "Testing")
{
myAudio.clip = Songs[0];
myAudio.Play();
}
}
}
I tried looking through unity docs but to no avail. I think the reason behind this is that when the audioSource doesn't have a clip, the stop() command get queued, then when you tell it to play it automatically stops right after. Unity can be weird sometimes.
Answer by SpecticalPro · Jun 24, 2013 at 06:07 PM
using UnityEngine;
using System.Collections;
public class MusicChange : MonoBehaviour {
public AudioClip[] Music;
private AudioSource myAudio;
// Use this for initialization
void Start () {
myAudio = audio;
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if(gameObject.tag == "Testing")
{
if(myAudio.clip != Music[0])
{
myAudio.Stop();
if(other.gameObject.tag == "Player")
{
myAudio.clip = Music[0];
myAudio.Play();
}
}
}
if(gameObject.tag == "Overworld")
{
if(myAudio.clip != Music[1])
{
myAudio.Stop();
if(other.gameObject.tag == "Player")
{
myAudio.clip = Music[1];
myAudio.Play();
}
}
}
}
}
Same comment as above, just noticed I typed it on the wrong one:
Unfortunately it doesn't work this way either. :(
A few quirks I've noticed:
1) When my player walks through the FIRST trigger... nothing happens. When he walks into the second, the audio will play for just one blip and then cease.
2) When I changed "$$anonymous$$usic[0]" and "$$anonymous$$usic[1]" to just "audio" in general, I noticed for some reason that the audio didn't play for the first trigger, but played completely for the second!
3) The music that started playing for the 2nd trigger will stop when I enter into the first trigger or any other, including my camera bounds. I went ahead and turned off the camera and played from the scene view and confirmed.
4) I fiddled with the tags to try to figure out more; switched "Testing" / "Overworld" with Player and put it only on the Player. Worked the same as above.
Reading the code I feel it SHOULD be working, I don't know why it isn't! What in my code would cause ANY trigger to stop the audio?
One thing I just noticed, and its probably nothing, but the array name "$$anonymous$$usic" is shaded as a keyword, which is weird and may just be something to do with this forum, but due to lack of other ideas try changing this to something else, maybe "clips" or something.
No dice. :\ Good thinking though.
I've been tinkering more. Turns out when I remove one of the if statements entirely, the remaining if statement will play. I have no idea what in the world would cause it to not work if I have two identical sections of code. The only things that change are the tags and the songs!
void OnTriggerEnter(Collider other) { if(gameObject.tag == "Player") { if(myAudio.clip != Songs[0]) { myAudio.Stop(); audio.Stop (); if(other.gameObject.tag == "Testing") { myAudio.clip = Songs[0]; myAudio.Play(); } } }
if(gameObject.tag == "Player")
{
if(myAudio.clip != Songs[1])
{
myAudio.Stop();
audio.Stop();
if(other.gameObject.tag == "Overworld")
{
myAudio.clip = Songs[1];
myAudio.Play();
}
}
}
}
Answer by zerodragonheart · Jun 16, 2016 at 06:07 AM
So,, well i found out that maybe all of you have already solved this, but i managed to solve this issue with the following script: You only have to put the AUDIO to stop in the STOP box and the one you want to play in the PLAY box of the inspector. I use this cuss wn the player walks into the TRIGGER SONG it happens.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class MusicSwitch : MonoBehaviour
{
public AudioSource play;
public AudioSource stop;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
stop.Stop();
play.Play();
}
}
}
Your answer
Follow this Question
Related Questions
Stop sound on input.getkeyup 1 Answer
Play Audio on Collision or Trigger Enter 4 Answers
Don't destroy a variable when changing scene 2 Answers
Why isnt OnTriggerEnter Tags working. 1 Answer
How to stop Audio not all?? 0 Answers