How to stop playsample before it ends[SOLVED]

Steve Craft

Member
I'm in doubt if playsample actually have a way to stop it while playing it instead of having to wait for it to end,i searched on legacy manual but i didn't found anything related to it
 
Steve Craft,

You can pause or stop a sound channel, you just have to know which one. OpenBOR can play up to 64 samples simultaneously, and does this by assigning a playback job to the first available channel.

When you run the playsample() function, it returns the channel ID assigned for playback. You need to store that in a variable, and from there you can stop the channel using stopchannel(<channel id>).

Code:
int sound_id = loadsample("some_sound_file_path");

int channel_id = playsample(sound_id, <priority>, <left vol>, <right vol>, <speed>, <loop>);

setglobalvar("channel_to_stop", channel_id);

Then somewhere else...

Code:
int channel_id = getglobalvar("channel_to_stop");

stopchannel(channel_id);

setglobalvar("channel_to_stop", NULL());

Note you can do the same thing with native sound playback, but it's going to be clunky since there's no way to know what channel was assigned. You'd need to run a loop to compare sound files or sound IDs in every channel until you found the one you wanted.

HTH,
DC
 
I been trying like this and it don't stop the sample
Code:
 if(Animation == openborconstant("ANI_IDLE") ){
      if(PAttack){
        int Charging1 = loadsample("data/MMXSound/MMXCharge.wav");
        int channel_id = playsample(Charging1,0,120,120,100,0);
        playsample(Charging1,0,120,120,100,0);
        setglobalvar("channel_to_stop", channel_id);
        performattack(Self, openborconstant("ANI_FREESPECIAL"), 1);
      }
   }

      if(Animation == openborconstant("ANI_FREESPECIAL") ){
          if(NotPAttack){
          int channel_id = getglobalvar("channel_to_stop");
          stopchannel(channel_id);
          setglobalvar("channel_to_stop", NULL());  
          int ChargeShot = loadsample("data/MMXSound/ChargeShot.wav");
          performattack(Self, openborconstant("ANI_FOLLOW1"), 0);
          playsample(ChargeShot,0,120,120,100,0);
        }
      }
     }
is it possible to make it work on keyscript or i'm supposed to use it on somewhere else?
 
You're running playsample() for the same sound twice in a row, and you're only getting the channel ID from first one. Remove the second playsample().

DC
 
Steve Craft,

Steve Craft said:
just one question about "channel_to_stop",do i need to put this exact name for the globalvar to stop it or i can use any name that i want?

That code I gave you is just a quick example, I didn't mean for you to take it literally. You can use any name you like. It also doesn't need to be a global var depending on when/where your functions are at. See here for details.

DC
 
Back
Top Bottom