• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Zib Redlektab · Nov 24, 2010 at 11:10 PM · audiopausemultitaskingunpause

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zib Redlektab · Dec 01, 2010 at 05:07 AM 0
Share

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

avatar image rainyy · Mar 26, 2011 at 03:54 AM 0
Share

i need the JS version! Thanks :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to know if Audio is Paused? 2 Answers

pause a audio inside game 1 Answer

How do I pause and unpause only some GameObjects at a time 1 Answer

Music On/Off Switch 2 Answers

why cant i play my scene? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges