Reputation: 720
Title is pretty self explanatory. But here's a brief description of my problem:
I'm using playscheduled to switch tracks on beat/bar. The issue is that sometimes, some event happens and I would like to cancel a specific queued track from being played.
Using AudioSource.Stop() will only stop the currently played track, the scheduled tracked will still play afterwards.
Any ideas? Is there a method to call that I am missing?
Upvotes: 1
Views: 985
Reputation: 720
After much trial and error, I after creating a whole new scene to test the various options I have realized that AudioSource.Stop() WILL stop a scheduled track from playing. It was my error that was causing my queueing system to schedule a new track after I have sent a cancel call.
To see this working just schedule a track and print AudioSource.isPlaying. You will notice that even thou the source is not currently playing, scheduling it will set this variable to "true". Once you call AudioSource.Stop(), the variable will be set back to false and the scheduled track will never play.
Upvotes: 1
Reputation: 13146
You can use a coroutine to control when and if the next clip should be played. There is a simple example in the AudioSource.clip reference that needs to be extended a little bit.
The following code snippet plays the clip attached to AudioSource
and ToggleAudioClips.otherClip
alternating in an endless loop until ToggleAudioClips.StopPlaying ()
is called. A new coroutine is started whenever the current clip is finished.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(AudioSource))]
public class ToggleAudioClips : MonoBehaviour
{
public AudioClip otherClip;
bool stopPlaying = false;
AudioClip currentClip;
AudioClip nextClip;
void Start () {
currentClip = audio.clip;
nextClip = otherClip;
StartCoroutine (PlayClip ());
}
IEnumerator PlayClip () {
audio.clip = currentClip;
audio.Play ();
yield return new WaitForSeconds(audio.clip.length);
if (!stopPlaying) {
AudioClip c = nextClip;
nextClip = currentClip;
currentClip = c;
StartCoroutine (PlayClip ());
}
}
public bool StopPlaying () {
stopPlaying = true;
audio.Stop ();
}
}
Upvotes: 1