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*.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*.Show(b);_
    _
    }*_

}

Read this first

You can use [AudioListener.pause][1] 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

I used [AudioSource.time][2] 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*.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*.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;
}
}
_[1]: http://docs.unity3d.com/Documentation/ScriptReference/AudioListener-pause.html*_
_
[2]: http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-time.html*_

WebGL made those music out of sync when pausing.