Stopping a sound file

Hi folks,

I was wondering if anyone knows how to stop a sound file once it has begun to play?

I have the following function:

def play_sound_stimulus(self, soundfile, vol):
    play_audio1 = sound.Sound(value = soundfile)
    play_audio1.setVolume(vol)
    play_audio1.play()

And I execute it as follows:

self.play_sound_stimulus('StereoWN.wav', .05)

I was wondering how I can go about simply stopping the file (once a specific if condition I have written is met)?

I can’t see anything pertaining to sound files (I understand there is a stop/duration option for generated sound).

Thanks for your time.

Sound objects have a .stop() method. (Sound files are just files: you can’t stop one until it is turned into a sound object).

But as you have hidden the play_audio1 object away in a function, there is no way to refer to it outside the function.

You should have your function return play_audio1 so that you can send it a .stop() message later as required.

Thank you that’s perfect. Got it working now.

1 Like