Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by serenefox · Mar 16, 2013 at 11:28 PM · audiopauseresume

Problem with audio after leaving pause menu

Hello,

A while ago someone on here helped me fix this script(below) for which I am very greatful, but we couldn't get the audio to resume from where it was when the pause menu was exited. The music just started over. Which wasn't much of a problem until now. The reason it is now is beacuse I have more sounds in there so when the pause menu is exited all the ingame sounds play. I believe it has something to do with the array of audio sources that is a private variable, but if I change that then I have a feeling they will not stop at all when the pause menu is entered. All I want is for the sounds to contiune to stop when paused but when unpaused they continue from where they left off, or not play until they are supposed to. How would I go about this? Does anyone have an example? This is the script im using.

 var pause : boolean = false;
 var clickTexture : ClickTexture[];
 private var allAudioSources : AudioSource[];
 
 function Awake() 
 {
     EnableClick( false );
     
     for( var i : int = 0; i < clickTexture.Length; i++ )
     {
         clickTexture[i].pauseScript = this;
     }
     
     allAudioSources = FindObjectsOfType(AudioSource) as AudioSource[];
 }
 
  function PauseAllAudio() 
  {
          for(var audioS : AudioSource in allAudioSources) 
          { 
              audioS.Pause();
          }
  }
  
  function ResumeAllAudio() 
  {
      for(var audioS : AudioSource in allAudioSources)
          { 
          audioS.Play();
          }
  
  }
 
  
  function Update()
  {
      if(Input.GetKeyUp(KeyCode.Escape)) 
      {
          pause = !pause;
          
      
          if(pause == true) 
          {
              Time.timeScale = 0.0; 
              EnableClick( true ); 
              PauseAllAudio();
          }
          else
          {
              Time.timeScale = 1.0;
              EnableClick( false );
              ResumeAllAudio();
          }
      }
  }
  
 function RunOption( str : String )
 {
     if( str == "quit" )
     {
         Time.timeScale = 1.0;
         Application.LoadLevel("AliensMainMenu");
     }
      else if( str == "resume" )
     {
         Time.timeScale = 1.0;
         EnableClick( false );
         ResumeAllAudio();
     }
 }
 
 function EnableClick( b : boolean )
 {
     for( var i : int = 0; i < clickTexture.Length; i++ ) 
     {    
         clickTexture[i].Show(b);
     }
     
 }
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 serenefox · Mar 16, 2013 at 11:33 PM 0
Share

Also I should mention that after I leave the pause menu and press "esc" to try and go back to it, it starts the music over again and doesn't pause until I press it one more time.

1 Reply

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

Answer by Chronos-L · Mar 17, 2013 at 04:30 AM

Read this first

ou can use AudioListener.pause to pause-resume audio. The answers and script below will work, but it will be overdoing it.











Overdid Answers/Scripts

I wrote a wrapper to control the basic flow of a audio (Play, Pause, Resume, Stop)

AudioPlayer Wrappper (CS)

/*using UnityEngine; using System.Collections;

 [RequireComponent(typeof(AudioSource))]
 public class AudioPlayer : MonoBehaviour {
    
    private float time;
    private bool canPlay;
     
    //Testing code, should be deleted
    void Update() {
       if( Input.GetKeyDown(KeyCode.P) ) {
          Pause ();    
       }
         
       if( Input.GetKeyDown(KeyCode.O) ) {
          Resume ();    
       }
    }
     
    void Stop() {
       audio.Stop();
       time = 0;
       canPlay = false;
    }
     
    void Play() {
       audio.Play();
       time = 0;
       canPlay = true;
    }
         
    void Pause() {
      canPlay = audio.isPlaying;        
      time = audio.time;        
      audio.Stop();
    }   
     
    void Resume() {
       if( canPlay ) {
          audio.Play();
          audio.time = time;
       }
    }
 }*/


AudioPlayer.js // Overdid Script

    /*private var time : float;
    private var canPlay : boolean;
  
    function Stop() {
       audio.Stop();
       time = 0;
       canPlay = false;
    }
  
    function Play() {
       audio.Play();
       time = 0;
       canPlay = true;
    }
  
    function Pause() {
      canPlay = audio.isPlaying;     
      time = audio.time;     
      audio.Stop();
    }   
  
    function Resume() {
       if( canPlay ) {
          audio.Play();
          audio.time = time;
       }
    }*/

How to use

You will have to attach this script to all your audio source. I have tested this with 3 types of audio: a 3 minute song, a looping short-loop audio, and a one-shot death-cry.

Explanation

used AudioSource.time to make the audio player resume-able. I grab the audio.time right before it is Stop(), if I do it after, I will always get 0 for the audio.time. When I Resume(), I Play() the audio and set the audio.time to match the last time I Pause().

I use a boolean canPlay to determine whether the audio (this is meant for the one-shot audio) should be played when it is Resume(). I grab the audio.isPlaying when I Pause(). When the one-shot audio is not done playing yet, it will continue playing when I Resume().




Pause.js

 #pragma strict
  
 var pause : boolean = false;
 var clickTexture : ClickTexture[];
 //var allAudioSources : AudioPlayer[];
  
 function Awake() 
 { 
     EnableClick( false );
  
     for( var i : int = 0; i < clickTexture.Length; ++i ) 
       clickTexture[i].pauseScript = this;
  
     //allAudioSources = FindObjectsOfType(AudioPlayer) as AudioPlayer[];
 }
  
  function PauseAllAudio() 
 {
     /*for(var audioS : AudioPlayer in allAudioSources) { 
         if( audioS != null ) {
             audioS.Pause(); 
         }
     }*/

     AudioListener.pause = true;
 }
  
 function ResumeAllAudio() 
 {
     /*for(var audioS : AudioPlayer in allAudioSources) { 
         if( audioS != null ) {
             audioS.Resume(); 
         }
     }*/
     AudioListener.pause = false;
 }
  
 function Update()
 {
      if(Input.GetKeyUp(KeyCode.Escape)) 
      {
         //Super lazy one-liner
         pause = !pause; 
  
         if(pause == true) 
         {
           Time.timeScale = 0.0;
           EnableClick( true );    
           PauseAllAudio();
         }
         else 
         {
           Time.timeScale = 1.0;
           EnableClick( false );    
           ResumeAllAudio();
         }
      } 
 }
  
 function RunOption( str : String ) {
    if( str == "quit" ) {
        Application.LoadLevel("AliensMainMenu");
    }
    else if( str == "resume" ) {
       pause = false;
             
       Time.timeScale = 1.0;
       EnableClick( false );    
       ResumeAllAudio();
    }
 }
  
 function EnableClick( b : boolean ) {
    for( var i : int = 0; i < clickTexture.Length; ++i ) 
       clickTexture[i].Show(b);
 }


ClickTexture.js

 #pragma strict
 
 var pauseScript : Pause;
 var optionString : String;
  
 function OnMouseEnter()
 {
    //change text color
    guiTexture.color = Color.red;
 }
  
 function OnMouseExit()
 {
  
    //change text color
    guiTexture.color = Color.white;
 }
  
 function OnMouseUp()
 {
    pauseScript.RunOption( optionString );
 }
 
 function Show( b : boolean ) {
    this.enabled = b;
     
    if( guiTexture ) {
       guiTexture.enabled = b;
    }
     
    if( guiText ) {
       guiText.enabled = b;
    }
 }


Comment
Add comment · Show 17 · 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 Chronos-L · Mar 17, 2013 at 04:40 AM 0
Share

@serenfox, test it first before you integrate it into your code. I whip it up in a short while and test it with only 3 different audio.

If you are using this one, you should change your script like so:

 ...
 private var allAudioSources : AudioPlayer[];
  
 function Awake() 
 {
     ...
  
     allAudioSources = FindObjectsOfType(AudioPlayer) as AudioPlayer[];
 }
  
 function PauseAllAudio() 
 {
    for(var audioS : AudioPlayer in allAudioSources ) 
    { 
       audioS.Pause();
    }
 }
  
 function ResumeAllAudio() 
 {
    for(var audioS : AudioPlayer in allAudioSources )
    { 
       audioS.Resume();
    }
 }
  
  
 function Update()
 {
    ...
 }
  
 function RunOption( str : String )
 {
    ...
 }
  
 function EnableClick( b : boolean )
 {
    ...
 }
avatar image serenefox · Mar 17, 2013 at 05:50 AM 0
Share

Thank you for the reply, but I have a question. The first code that is in CS, am I supposed to attach that to my gameobject that has the audio source attached to it? Then access it with the second code which is JS? Can you access a CS script by a JS? Or is it just the javascript I modify and not even use the CS?

avatar image Chronos-L · Mar 17, 2013 at 05:59 AM 0
Share

The first code that is in CS, am I supposed to attach that to my gameobject that has the audio source attached to it?

Yes


Then access it with the second code which is JS?

Yes


Can you access a CS script by a JS?

Yes. Look at this. However, it is dead easy to convert this script to JS

  • have edited my answer to put in a JS version.*


Or is it just the javascript I modify and not even use the CS?

Use the AudioPlayer script, and make the appropriate changes to your Pause script as well.

avatar image serenefox · Mar 17, 2013 at 07:01 AM 0
Share

Again thank you for replying and explaining. I have made the changes but now I get this error: $$anonymous$$issingReferenceException: The object of type 'AudioPlayer' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

on this line: canPlay = audio.isPlaying;

avatar image serenefox · Mar 17, 2013 at 07:08 AM 0
Share

Also when I press the escape key after exiting the pause menu it restarts everythinbg again. is the escape key already used for something? Is that why its doing that?

Show more comments

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

11 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

Related Questions

how to pause and resume music after pausing game? 4 Answers

Audio keeps playing when paused 1 Answer

Pause and resume coroutine 1 Answer

Audio Issue 1 Answer

Pause game-resume causing event to fire 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