- Home /
Audio un-pauses when game gets focus
I've implemented a pausing system (in javascript) that uses AudioListener.pause = true in order to temporarily stop all sounds that are currently playing. This works fine for the most part, but I've found that when switching to a different application, and then returning to the game, the audio will resume playing, even if the game is still in its "paused" state.
I've managed to limit this for the most part by checking if the game should be paused at the beginning of Update, and using AudioListener.pause again, if it should be. However, you still hear a split-second burst of audio before it pauses. It's not really enough to be noticeable, but if you switch out and in enough times, the audio stops synching up with its subtitles.
Has anybody else encountered this, or have an idea for how I could be pausing better, so my AudioListener stays paused?
Answer by fireDude67 · Dec 01, 2010 at 04:44 AM
bool hasFocus = true;
void OnApplicationFocus() { hasFocus != hasFocus; if (!hasFocus) AudioListener.pause = true; // Or whatever you do to pause }
void Update() { if (GameIsPaused) AudioListener.pause = true; else if (AudioListnener.pause) AudioListener.pause = false; else return; }
JavaScript:
var hasFocus : bool = true;
function OnApplicationFocus() [ hasFocus = != hasFocus; if (!hasFocus) AudioListner.pause = true; // Or whatever you do to pause }
function Update() { if (GameIsPaused) AudioListner.pause = true; else if (AudioListner.pause) AudioListner.pause = false; else return; }
Let me know if you need a JavaScript version
Thanks! Did not know about OnApplicationFocus at all! :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613084416im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to know if Audio is Paused? 2 Answers
Audio keeps playing when paused 1 Answer
audio.ignoreListenerPause 1 Answer
Mute/Unmute not working with new sounds 2 Answers
How to continue paused audio? Is there a special function? 1 Answer