0

I can't use the playsound on Linux for some reason, I tried:

sudo apt install gstreamer1.0-gtk3

But still not working.

The code is pretty simple:

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')

The error:

<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'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

2 Answers2

1

Hello I had to change the way I play the audio in Ubuntu18, the file is:

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

and install mpg123:

sudo apt-get install -y mpg123

the change line 149 and 148

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

Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
0

You have to add the path of the audio file. eg. audio_filename = "/home/assistant/audio.mp3" and tts.save(audio_filename)

RG-SW
  • 1
  • 1