简体   繁体   English

Python WinSound:写入wav后出现运行时错误

[英]Python WinSound: Runtime Error after writing to wav

I'm trying to make a program that plays text-to-speech with gTTS. 我正在尝试制作一个使用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. 当我在Media Player中打开文件时,它工作正常。 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. 这是一种无需winsound的方法,它可以打开媒体播放器,然后为您和您播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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