Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 castor · Oct 04, 2013 at 08:06 PM · audiopauseisplayingunpause

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();





Comment
Add comment · Show 1
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 huulong · Jan 26, 2015 at 11:10 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · 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
0

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();
        }
     }
 
Comment
Add comment · Show 4 · 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 castor · Oct 04, 2013 at 08:23 PM 0
Share

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.

avatar image jonbro5556 · Oct 04, 2013 at 09:43 PM 0
Share

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();
 
avatar image castor · Oct 11, 2013 at 04:59 PM 0
Share

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?

avatar image huulong · Jan 26, 2015 at 11:14 AM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio un-pauses when game gets focus 1 Answer

Main Menu in different scene - pause game 3 Answers

Pause Menu Audio 1 Answer

Pausing and unpausing the game with one button 2 Answers

4.1 audio pause set to true on level load? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges