Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Rocco on Tue 10/07/2012 16:58:42

Title: need AudioClip.IsPlaying
Post by: Rocco on Tue 10/07/2012 16:58:42
hmm, i remember this was easier with the old audiosystem.

all i want to do, is play an audioclip, but take care if it is playing already.
So i have:
Code (AGS) Select

// ROOMSCRIPT
AudioChannel *channel;

function room_RepExec()
{
   if(channel)
    {
      if(!channel.IsPlaying)
        channel = aJB_crash_small.Play();
    }   
    else
      channel = aJB_crash_small.Play();
}


this construct doesnt work, i dont know why?



Title: Re: need AudioClip.IsPlaying
Post by: monkey0506 on Tue 10/07/2012 17:13:20
What does it mean that it's "not working"? Is it constantly restarting the clip, not playing it at all, playing it multiple times??

Regarding AudioClip.IsPlaying, you could do:

Code (ags) Select
import bool IsPlaying(this AudioClip*, int channel=-1);

bool IsPlaying(this AudioClip*, int channel)
{
  if (channel != -1)
  {
    return (System.AudioChannels[channel].PlayingClip == this);
  }
  channel = 0;
  while (channel < System.AudioChannelCount)
  {
    if (System.AudioChannels[channel].PlayingClip == this) return true;
    channel++;
  }
  return false;
}


Usage:

Code (ags) Select
if (aJB_crash_small.IsPlaying())
{
  // do something about it
}
Title: Re: need AudioClip.IsPlaying
Post by: Rocco on Tue 10/07/2012 17:23:35
Quote from: monkey_05_06 on Tue 10/07/2012 17:13:20
What does it mean that it's "not working"? Is it constantly restarting the clip, not playing it at all, playing it multiple times??
No there was silence, it wasnt playing.

thx for your function, this is a great solution,
but i got an error message on line 7
Code (AGS) Select
return (System.AudioChannels[channel].PlayingClip == this);
Error (line 7): must have an instance of the struct to access a non-static member
Title: Re: need AudioClip.IsPlaying
Post by: Crimson Wizard on Tue 10/07/2012 17:32:40
Corrected script:

Code (ags) Select
import bool IsPlaying(this AudioClip*, int channel=-1);

bool IsPlaying(this AudioClip*, int channel)
{
  if (channel != -1)
  {
    AudioChannel * ch = System.AudioChannels[channel];
    return (ch.PlayingClip == this);
  }
  channel = 0;
  while (channel < System.AudioChannelCount)
  {
    AudioChannel * ch = System.AudioChannels[channel];
    if (ch.PlayingClip == this) return true;
    channel++;
  }
  return false;
}


These are AGS compiler limitations.
Title: Re: need AudioClip.IsPlaying
Post by: monkey0506 on Tue 10/07/2012 17:40:33
Right! I always forget about that.
Title: Re: need AudioClip.IsPlaying
Post by: Rocco on Tue 10/07/2012 17:59:33
Thx, the function works now without errors, but unfortunatly its not working accurate.
Maybe its because there are more sounds running at once.
There is a constant driving sound.clip which changes with different speed, and the crash sound is an additional sound.
I guess the function gives the wrong channel back sometimes, so the sound is playing multiple times alternatly with stopping and playing only once.
Title: Re: need AudioClip.IsPlaying
Post by: monkey0506 on Tue 10/07/2012 18:41:13
There's only 8 audio channels shared between music, sounds, and speech. I don't know what you mean by "the function gives the wrong channel back". If you're referring to the custom AudioClip.IsPlaying() function, it doesn't return an AudioChannel at all, and I can assure you that even if it did, it wouldn't return the wrong channel.

Beyond that extender method, what other code are you currently using?
Title: Re: need AudioClip.IsPlaying
Post by: Rocco on Tue 10/07/2012 19:19:32
i see, this with the wrong channel was nonsens, sorry.
Big Thx you both its working now properly,  ;-D
i had an issue with the rpm sounds, too many were playing at once,
what was probably also the reason why my construct from the 1st Post didnt work.