Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Ghostlady on Thu 07/07/2011 02:01:47

Title: Pause Audio
Post by: Ghostlady on Thu 07/07/2011 02:01:47
Is there a way to pause the audio while a "Save/Load/Quit" gui is up?  If I stop the audio and the player decides to return to "play" or "save" then it is kinda tricky knowing which audio to start up again (don't have all the music on channels) since that gui can come up anywhere in the game.
Title: Re: Pause Audio
Post by: monkey0506 on Thu 07/07/2011 02:12:43
There's no built-in method, but you can script one:

// Script.ash

import void PauseAudio(AudioType);
import void UnPauseAudio();

// Script.asc

AudioClip *pausedAudio;
int pausedAudioPosition;

void PauseAudio(AudioType type)
{
 int i = 0;
 while (i < System.AudioChannelCount)
 {
   if ((System.AudioChannels[i].IsPlaying) && (System.AudioChannels[i].PlayingClip != null) && (System.AudioChannels[i].PlayingClip.Type == type))
   {
     pausedAudio = System.AudioChannels[i].PlayingClip;
     pausedAudioPosition = System.AudioChannels[i].Position;
     System.AudioChannels[i].Stop();
     return;
   }
   i++;
 }
}

void UnPauseAudio()
{
 if (pausedAudio == null) return;
 pausedAudio.PlayFrom(pausedAudioPosition);
 pausedAudio = null;
 pausedAudioPosition = 0;
}


It's somewhat basic in that it will only pause one instance of the specified type, but it should work for your music (which is what you wanted it for, right?).

Usage is like this:

// show GUI:
gMenu.Visible = true;
PauseAudio(eAudioTypeMusic);

// hide GUI:
gMenu.Visible = false;
UnPauseAudio();
Title: Re: Pause Audio
Post by: Wyz on Thu 07/07/2011 02:15:03
Hmmm, maybe it's an idea to turn down the volume? Then you would still have a gap but that might be not that noticeable.  :)
Title: Re: Pause Audio
Post by: Ghostlady on Thu 07/07/2011 02:25:48
Cool, I've give this a shot.  If it doesn't work, I'll use the volume. Ahem, didn't even think of that.
Title: Re: Pause Audio
Post by: Ghostlady on Thu 07/07/2011 03:40:26
On this line:

if ((System.AudioChannels.IsPlaying) && (System.AudioChannels.PlayingClip != null) && (System.AudioChannels.PlayingClip.Type == type))

I am getting an error msg.

must have an instance of the struct to access a non-static member
Title: Re: Pause Audio
Post by: monkey0506 on Thu 07/07/2011 04:11:29
Oh right, sorry. Replace that line with:

    AudioChannel *channel = System.AudioChannels[i];
    if ((channel.IsPlaying) && (channel.PlayingClip != null) && (channel.PlayingClip.Type == type))


Then replace the lines:

      pausedAudio = System.AudioChannels[i].PlayingClip;
      pausedAudioPosition = System.AudioChannels[i].Position;
      System.AudioChannels[i].Stop();


With:

      pausedAudio = channel.PlayingClip;
      pausedAudioPosition = channel.Position;
      channel.Stop();
Title: Re: Pause Audio
Post by: Ghostlady on Fri 08/07/2011 01:35:16
Works like a charm!!!
Title: Re: Pause Audio
Post by: monkey0506 on Fri 08/07/2011 06:43:52
Yes, sometimes I forget about that compiler limitation, but it's simple enough to get around it. In any case, glad to hear you got it working! :)
Title: Re: Pause Audio
Post by: Ghostlady on Mon 02/07/2012 19:51:29
I need some help with this code.  I've used the above code and most of the time it works.  There are occasions when I come back from the Save and the music is no longer playing.  What am I missing?