Pause Audio

Started by Ghostlady, Thu 07/07/2011 02:01:47

Previous topic - Next topic

Ghostlady

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.
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven Plantation
Haunting at Cliffhouse

monkey0506

#1
There's no built-in method, but you can script one:

Code: ags
// 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:

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

// hide GUI:
gMenu.Visible = false;
UnPauseAudio();

Wyz

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.  :)
Life is like an adventure without the pixel hunts.

Ghostlady

Cool, I've give this a shot.  If it doesn't work, I'll use the volume. Ahem, didn't even think of that.
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven Plantation
Haunting at Cliffhouse

Ghostlady

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
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven Plantation
Haunting at Cliffhouse

monkey0506

Oh right, sorry. Replace that line with:

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


Then replace the lines:

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


With:

Code: ags
      pausedAudio = channel.PlayingClip;
      pausedAudioPosition = channel.Position;
      channel.Stop();

Ghostlady

Works like a charm!!!
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven Plantation
Haunting at Cliffhouse

monkey0506

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! :)

Ghostlady

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?
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven Plantation
Haunting at Cliffhouse

SMF spam blocked by CleanTalk