Author Topic: need AudioClip.IsPlaying  (Read 479 times)  Share 

need AudioClip.IsPlaying
« on: 10 Jul 2012, 16:58 »
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: Adventure Game Studio
  1. // ROOMSCRIPT
  2. AudioChannel *channel;
  3.  
  4. function room_RepExec()
  5. {
  6.    if(channel)
  7.     {
  8.       if(!channel.IsPlaying)
  9.         channel = aJB_crash_small.Play();
  10.     }    
  11.     else
  12.       channel = aJB_crash_small.Play();
  13. }
  14.  
  15.  
this construct doesnt work, i dont know why?




monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: need AudioClip.IsPlaying
« Reply #1 on: 10 Jul 2012, 17:13 »
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: Adventure Game Studio
  1. import bool IsPlaying(this AudioClip*, int channel=-1);
  2.  
  3. bool IsPlaying(this AudioClip*, int channel)
  4. {
  5.   if (channel != -1)
  6.   {
  7.     return (System.AudioChannels[channel].PlayingClip == this);
  8.   }
  9.   channel = 0;
  10.   while (channel < System.AudioChannelCount)
  11.   {
  12.     if (System.AudioChannels[channel].PlayingClip == this) return true;
  13.     channel++;
  14.   }
  15.   return false;
  16. }

Usage:

Code: Adventure Game Studio
  1. if (aJB_crash_small.IsPlaying())
  2. {
  3.   // do something about it
  4. }
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.

Re: need AudioClip.IsPlaying
« Reply #2 on: 10 Jul 2012, 17:23 »
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: Adventure Game Studio
  1. return (System.AudioChannels[channel].PlayingClip == this);
Error (line 7): must have an instance of the struct to access a non-static member

Crimson Wizard

  • AGS Project Admins
  • not et suppotreD
    • I can help with translating
    •  
Re: need AudioClip.IsPlaying
« Reply #3 on: 10 Jul 2012, 17:32 »
Corrected script:

Code: Adventure Game Studio
  1. import bool IsPlaying(this AudioClip*, int channel=-1);
  2.  
  3. bool IsPlaying(this AudioClip*, int channel)
  4. {
  5.   if (channel != -1)
  6.   {
  7.     AudioChannel * ch = System.AudioChannels[channel];
  8.     return (ch.PlayingClip == this);
  9.   }
  10.   channel = 0;
  11.   while (channel < System.AudioChannelCount)
  12.   {
  13.     AudioChannel * ch = System.AudioChannels[channel];
  14.     if (ch.PlayingClip == this) return true;
  15.     channel++;
  16.   }
  17.   return false;
  18. }

These are AGS compiler limitations.

monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: need AudioClip.IsPlaying
« Reply #4 on: 10 Jul 2012, 17:40 »
Right! I always forget about that.
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.

Re: need AudioClip.IsPlaying
« Reply #5 on: 10 Jul 2012, 17:59 »
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.

monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: need AudioClip.IsPlaying
« Reply #6 on: 10 Jul 2012, 18:41 »
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?
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.

Re: need AudioClip.IsPlaying
« Reply #7 on: 10 Jul 2012, 19:19 »
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.
« Last Edit: 10 Jul 2012, 19:21 by Rocco »