简体   繁体   中英

how to stop/close an audio file(mp3/.wav) using python script

I wrote code to play an audio file using python, like below.

def playSound(self):
    os.system('start C:\\Users\\unavaras\\Music\\223.wav')
    time.sleep(10)

It's playing the audio file recursively and it's not being closed untill i manually close it.

Tried below code as well to stop audio, but didn't work

def playSound(self):
    p1 = Popen('start C:\\Users\\unavaras\\Music\\223.wav', shell=True)
    time.sleep(5)
    p1.kill()

In both cases audio is being played recursively.

Can some help me how I can stop the audio after some time or once it finishes playing.

I am using linux but you can try this

def playSound(self):
     os.system('timeout 10 start C:\\Users\\unavaras\\Music\\223.wav')

For finding the time duration of audio this link Get .wav file length or duration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM