0

由于某种原因,我无法在 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'
4

2 回答 2

0

您必须添加音频文件的路径。例如。audio_filename = "/home/assistant/audio.mp3" 和 tts.save(audio_filename)

于 2021-04-24T14:11:33.680 回答
0

你好我不得不改变我在 Ubuntu18 中播放音频的方式,文件是:

..../anaconda3/envs/Jarvis/lib/python3.6/site-packages/JarvisAI/ init .py

并安装mpg123

sudo apt-get install -y mpg123

更改线 149 和 148

playsound("tmp.mp3") 与 os.system('mpg123 -q tmp.mp3')

于 2021-05-20T20:34:04.750 回答