How do I stop playing music with simple audio?

Question:

How do you stop playing audio with simpleaudio?

def audio_1():
    wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
    play_obj = wave_obj.play()

I have a function here that plays the audio and is connected to a Tkinter button. Right now I need a function to stop playing any audio.

Asked By: Dav_Did

||

Answers:

See simpleaudio.

As suggested in the earlier comment, you can do it using wave_obj.stop or sa.stopall() which will stop all currently playing audio.

Answered By: asha ganapathy

You would either use wave_obj.stop() to stop that specific audio, or you can use sa.stopall(), which will stop all audio. The code should either look like this:

def audio_1():
    wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
    play_obj = wave_obj.play()

    # Play for as long as you want
    stop_obj = wave_obj.stop()

Or this:

def audio_1():
    wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
    play_obj = wave_obj.play()

    # Play for as long as you want
    sa.stopall()
Answered By: user14463336

You just have to call .stop() on the play object:

def audio_1():
    wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
    play_obj = wave_obj.play()
    play_obj.stop()
Answered By: a door
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.