how to play mp3

前端 未结 12 1682
醉酒成梦
醉酒成梦 2021-02-09 09:39

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

相关标签:
12条回答
  • 2021-02-09 10:10

    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.

    0 讨论(0)
  • 2021-02-09 10:12
        import os
        os.system("music.mp3")
    
    0 讨论(0)
  • 2021-02-09 10:13

    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.

    0 讨论(0)
  • 2021-02-09 10:17

    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
    
    0 讨论(0)
  • 2021-02-09 10:17

    I think you are looking for :

    pyttsx3

    Its an offline cross-platform TTS that's compatible with python3 and python2.

    Install :

    pip install pyttsx3

    If u want an offline TTS for python unlike gTTS , pyttsx3 is your best choice in my opinion.

    0 讨论(0)
  • 2021-02-09 10:18

    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.

    0 讨论(0)
提交回复
热议问题