简体   繁体   中英

Python WinSound: Runtime Error after writing to wav

I'm trying to make a program that plays text-to-speech with gTTS.

import gtts
import winsound
import time

def playtts(strin):
    fl = gtts.gTTS(text = strin, lang = 'en')
    fl.save('temp.wav')
    time.sleep(3)
    winsound.PlaySound('temp.wav', winsound.SND_FILENAME | winsound.SND_NOSTOP | winsound.SND_NODEFAULT)

playtts("HELLO THERE")

When I run it, I get:

  File "[DATA EXPUNGED]", line 14, in <module>
    playtts("HELLO THERE")
  File "[DATA EXPUNGED]", line 12, in playtts
    winsound.PlaySound('temp.wav', winsound.SND_FILENAME | winsound.SND_NOSTOP | winsound.SND_NODEFAULT)
RuntimeError: Failed to play sound
>>> 

When I open the file in Media Player, it works just fine. Why is it raising errors?

import gtts
import winsound
import time,os

def playtts(strin):
    fl = gtts.gTTS(text = strin, lang = 'en')
    fl.save('temp.wav')
    time.sleep(3)
    os.system("start temp.wav")


playtts("whiskers whats up")

Here's a way to do it without winsound it opens a media player and then plays it for you and you.

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