0

I am trying the gTTS (Google Text To Speach) function in python, saving the mp3 file works (the file is being saved and can be played).

Now I am trying to play the file directly with the below code, but it is throwing an error

Code:

import gtts
import pyglet
import os
import time

text = ("Hello World")

obj = gtts.gTTS(text=text, lang='en')
speech_filename = 'c:/test_voice.mp3'
obj.save(speech_filename)

print("Play sound...")

music = pyglet.media.load(speech_filename, streaming=False)
music.play

sleep.time(music.duration) #prevent from killing
os.remove(speech_filename) #remove temp file

Error:

Traceback (most recent call last): File "C:\python\text-to-speach.py", line 16, in music = pyglet.media.load(speech_filename, streaming=False)

File "C:\Python\lib\site-packages\pyglet\media\sources\loader.py", line 63, in load source = get_source_loader().load(filename, file)

File "C:\Python\lib\site-packages\pyglet\media\sources\loader.py", line 84, in load return WaveSource(filename, file)

File "C:\Python\lib\site-packages\pyglet\media\sources\riff.py", line 200, in init

'AVbin is required to decode compressed media') pyglet.media.sources.riff.WAVEFormatException: AVbin is required to decode compressed media

user2520212
  • 121
  • 1
  • 1
  • 9

2 Answers2

1

it is looking for AVbin, check following would help you https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin

ShivYaragatti
  • 398
  • 2
  • 8
  • I installed AVbin library from [http://avbin.github.io/AVbin/Download.html] and had to do a simple adjustment to work on Windows 10 64bit - **How to fix it on Windows10** [https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin] But now time is showing an error **NameError: name 'sleep' is not defined** – user2520212 Jan 01 '19 at 14:57
  • 1
    try `time.sleep()` not sure `sleep.time()` is exist – ShivYaragatti Jan 01 '19 at 15:25
0

Ok so this is how I solved it, I also added a delay loop that waits for the audio file to finish and delete it afterwards.

import gtts
import pygame
#install pyglet and install http://avbin.github.io/AVbin/Download.html
#extract the avbin.dll from windows/system32/ folder to windows/system/ folder
import os
import time

pygame.mixer.init()

text = ("Hello, World")

obj = gtts.gTTS(text=text, lang='en')
speech_filename = "c:/python/code/test_voice.mp3"
obj.save(speech_filename)

print("Play sound...")

pygame.mixer.music.load(speech_filename)
pygame.mixer.music.play()

busy = True
while busy == True:
    if pygame.mixer.music.get_busy() == False:
        busy = False
pygame.quit()

os.remove(speech_filename) #remove temp file - remove line to keep file
user2520212
  • 121
  • 1
  • 1
  • 9