x


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?

more ▼

asked Nov 24 '10 at 11:10 PM

Zib Redlektab gravatar image

Zib Redlektab
304 10 12 24

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
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

more ▼

answered Dec 01 '10 at 04:44 AM

fireDude67 gravatar image

fireDude67
945 9 15 30

Thanks! Did not know about OnApplicationFocus at all! :D

Dec 01 '10 at 05:07 AM Zib Redlektab

i need the JS version! Thanks :)

Mar 26 '11 at 03:54 AM rainyy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1027
x261
x10
x8

asked: Nov 24 '10 at 11:10 PM

Seen: 1367 times

Last Updated: Nov 24 '10 at 11:10 PM