由于某些原因,我无法在Linux上使用播放声音,我尝试了:
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'
你好,我不得不改变在Ubuntu18中播放音频的方式,文件是:
/anaconda3/envs/Jarvis/lib/python3.6/site packages/JarvisAI/init.py
并安装mpg123
:
sudo apt-get install -y mpg123
变更线149和148
播放声音("tmp.mp3"(与操作系统('mpg123-q tmp.mp3'(
您必须添加音频文件的路径。例如。音频文件名="/home/assistant/audio.mp3";和tts.save(音频文件名(