- Home /
How to know if Audio is Paused?
I'm sure I'm overlooking something really obvious and couldn't find an answer around.
I have a generic list with all the audio in the game :
var worldAudioSources : List.<AudioSource>; //List of all audio sources in the game
And this is what I do when the player pauses:
for (var audio : AudioSource in worldAudioSources){
if (audio.isPlaying){
audio.Pause();
}
}
My problem is when I want to unPause ONLY the audio that is paused, but there doesn't seem to be a audio.isPaused?
for (var audio : AudioSource in worldAudioSources){
if (audio.isPaused){ //unfortunately doesnt exist
audio.Play();
}
}
UPDATE1 (not the greatest):
This is the best I could find so far (knowing if the audio is at 0 or not):
for (var audio : AudioSource in worldAudioSources){
if (audio.time != 0){
audio.Play();
}
}
UPDATE2 (best idea so far): A good suggestion from jonbro5556. Since it was added as a comment I'm adding it here.
"I think what you actually need to do is store in a separate list all of the audio clips that need to get restarted:"
var toRestart : List.<AudioSource>;
for (var audio : AudioSource in worldAudioSources){
if (audio.isPlaying){
toRestart.Add(audio);
audio.Pause();
}
}
then:
for (var audio : AudioSource in toRestart){
audio.Play();
}
toRestart.Clear();
The problem with the approach in the update is that, if the player pauses the game just when the audio starts, the audio source is technically playing (audio.isPlaying is true) but the audio.time and audio.timeSamples are still equal to 0. So when you resume the game, the audio will not be played at all. The second answer of jonbro5556 (register paused audio and resume playing later) worked for me.
Answer by jmorhart · May 25, 2015 at 09:01 PM
I've run into this problem as well. My solution was to create a Dictionary, where the Value of each Dictionary entry is the pause state of the AudioSource.
So I have code that looks something like this:
Dictionary<AudioSource, bool> pauseStates = new Dictionary<AudioSource, bool>();
// somewhere along the line add all your AudioSources to the dictionary,
// with their Values set to false
// pauseStates.Add(someAudioSource, false);
public void PauseAudio()
{
foreach (AudioSource source in pauseStates.Keys)
{
pauseStates[source] = source.isPlaying;
source.Pause();
}
}
public void ResumeAudio()
{
foreach (AudioSource source in pauseStates.Keys)
{
if (pauseStates[source])
{
source.Play();
}
pauseStates[source] = false;
}
}
So basically what's happening in the PauseAudio() function, if the source is currently playing, set its pause state to true. And then in the ResumeAudio() function, you only play the audio sources that were already paused.
Answer by jonbro5556 · Oct 04, 2013 at 08:17 PM
you can just invert the boolean value is "isPlaying" like so:
for (var audio : AudioSource in worldAudioSources){
if (!audio.isPlaying){
audio.Play();
}
}
That wouldn't work. Not all of the audio in the array is playing, some is stopped. If I use that boolean it will make them all play.
Ah, I actually misunderstood the question. I think what you actually need to do is store in a separate list all of the audio clips that need to get restarted:
var toRestart : List.<AudioSource>;
for (var audio : AudioSource in worldAudioSources){
if (audio.isPlaying){
toRestart.Add(audio);
audio.Pause();
}
}
then
for (var audio : AudioSource in toRestart){
audio.Play();
}
toRestart.Clear();
Yeah..that could work, still wondering if there is a simpler way native to Unity. If they implemented the pause, can't we know if something is still paused?
I did not find anything in Unity either, at least nothing clearly explained in the API. I decided to follow the answer in the comment, i.e. registering audio sources to resume playing later. Can you edit your main answer with the final code?
Your answer
Follow this Question
Related Questions
Audio un-pauses when game gets focus 1 Answer
Listen to audio when stepping frame by frame in editor 1 Answer
Audio is always playing 0 Answers
Script using a Pause library causes audio to intermittently cutout. 0 Answers
Pause Audio Help 1 Answer