stopping audio with playsound module

Kashish Jain picture Kashish Jain · Jul 23, 2019 · Viewed 18.6k times · Source

how do i stop the audio playing through playaudio module in python code I have played music= playsound.playsound("name_of-file") but I cant stop that music. how can I stop it

playsound.playsound("name_of_file")

Answer

Aakash Singh picture Aakash Singh · Apr 12, 2020

You can use the multiprocessing module to play the sound as a background process, then terminate it anytime you want:

import multiprocessing
from playsound import playsound

p = multiprocessing.Process(target=playsound, args=("file.mp3",))
p.start()
input("press ENTER to stop playback")
p.terminate()