由于某种原因,我无法在 Linux 上使用 playsound,我尝试过:
sudo apt install gstreamer1.0-gtk3
但仍然无法正常工作。
代码非常简单:
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang='en')
filename = 'voice.mp3'
tts.save(filename)
playsound.playsound(filename)
speak('hello')
错误:
<ipython-input-2-3bd586ed4a90> in <module>
5 playsound.playsound(filename)
6
----> 7 speak('hello tim')
<ipython-input-2-3bd586ed4a90> in speak(text)
3 filename = 'voice.mp3'
4 tts.save(filename)
----> 5 playsound.playsound(filename)
6
7 speak('hello tim')
~/anaconda3/envs/AI/lib/python3.8/site-packages/playsound.py in _playsoundNix(sound, block)
99 playbin.props.uri = sound
100 else:
--> 101 playbin.props.uri = 'file://' + pathname2url(os.path.abspath(sound))
102
103 set_result = playbin.set_state(Gst.State.PLAYING)
AttributeError: 'NoneType' object has no attribute 'props'