my problem starts here:
pyttsx and gTTS module errors
gTTS works well, takes text from text file, but first creates mp3 file, then if I want listen, I must call
If you are on windows then this should do the job,
import winsound
winsound.PlaySound('your_file.mp3',winsound.SND_FILENAME)
Remember this package is only present in python for windows.
import os
os.system("music.mp3")
install pyglet using python -m pip install pyglet
Download installed here Link to AVbin setup ( it's a must )
32/64 bit
import pyglet
song = pyglet.media.load('file.mp3')
song.play()
pyglet.app.run()
That's all.
This will do the job and without the need for files:
def say(text, lang='en'):
""" Speak the provided text.
"""
import pygame
from gtts import gTTS
import io
tts = gTTS(text=text, lang=lang, slow=False)
pygame.mixer.init()
pygame.init() # this is needed for pygame.event.* and needs to be called after mixer.init() otherwise no sound is played
with io.BytesIO() as f: # use a memory stream
tts.write_to_fp(f)
f.seek(0)
pygame.mixer.music.load(f)
pygame.mixer.music.set_endevent(pygame.USEREVENT)
pygame.event.set_allowed(pygame.USEREVENT)
pygame.mixer.music.play()
pygame.event.wait() # play() is asynchronous. This wait forces the speaking to be finished before closing f and returning
I think you are looking for :
Its an offline cross-platform TTS that's compatible with python3 and python2.
pip install pyttsx3
If u want an offline TTS for python unlike gTTS , pyttsx3 is your best choice in my opinion.
with pygame I have installation problem and not sure, how good is use it this way:
Can you provide more details about the pygame installation error?
I was able to use PYGAME with this code, where "hello.mp3" is a file in the same directory
from gtts import gTTS
tts = gTTS(text='Hello', lang='en')
tts.save("hello.mp3")
from Tkinter import *
root = Tk()
from pygame import mixer
mixer.init()
mixer.music.load('hello.mp3')
mixer.music.play()
root.mainloop()
vlc just how to install it?
I also used VLC. I have installed it with these commands:
sudo pip install python-vlc
And I got this error:
NameError: no function 'libvlc_new
So, I tryed the command:
sudo apt-get install vlc
And it worked with this code:
from gtts import gTTS
tts = gTTS(text='Hello', lang='en')
tts.save("hello.mp3")
from Tkinter import *
root = Tk()
import vlc
p = vlc.MediaPlayer("hello.mp3")
p.play()
root.mainloop()
Hope It help You.