need AudioClip.IsPlaying

Started by Rocco, Tue 10/07/2012 16:58:42

Previous topic - Next topic

Rocco

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

// 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?




monkey0506

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
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
if (aJB_crash_small.IsPlaying())
{
  // do something about it
}

Rocco

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
return (System.AudioChannels[channel].PlayingClip == this);

Error (line 7): must have an instance of the struct to access a non-static member

Crimson Wizard

Corrected script:

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

monkey0506

Right! I always forget about that.

Rocco

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.

monkey0506

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?

Rocco

#7
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.

SMF spam blocked by CleanTalk