Reputation: 79
Here is my code:
from gtts import gTTS
import pygame
def speak(audioString):
tts = gTTS(text=audioString, lang='en')
tts.save('audio.mp3')
pygame.mixer.init()
pygame.mixer.music.load('audio.mp3')
pygame.mixer.music.play()
speak('hello')
It creates the 'audio.mp3' file, but I can't hear anything. Any conjectures? Maybe I should use something else?
Upvotes: 0
Views: 232
Reputation: 46
Try converting the .mp3 file into .ogg - the latter extension is officially supported in the pygame library documents. The conversion solved one of the issues I had experienced.
Upvotes: 1
Reputation: 1296
play function is async and returns immediately, so you should add something to stall and wait before exiting.
And to be sure that you are not waiting so long, you can use this snippet:
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
Edit: there is even a better way to do this, use pygame.event.wait()
, which will wait for all the async tasks to end.
Upvotes: 2