how to play mp3

2019-04-07 02:32发布

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 this mp3, so it is good but it would be better if I can avoid any audio files, and get just read from text file. maybe somehow I can use google voice to read from text file..? anyway main question now is other

if I can use only gTTS what is the best way to play mp3 on Windows 10-64 bit, Python 3.5

ok with os:

import os
os.startfile("D:\my path/rec1.mp3") 

it is good, but I don't want use default player, need something like simpleaudio for mp3...

with pygame I have installation problem and not sure, how good is use it this way:

from pygame import mixer 

mixer.init()
mixer.music.load('D:/my path/rec1.mp3')
mixer.music.play()

vlc just how to install it? with easy_install vlc I got error: could not find suitable distribution for requirement.parse ('vlc') and with pip install vlc error: could not find a version that satisfies the requirement vlc (from versions: ) no matching distribution found for vlc

import vlc

p = vlc.MediaPlayer("file:/my path/rec1.mp3")
p.play()
p.stop()

with pyglet:

import pyglet

music=pyglet.media.load('D:/my path/rec1.mp3')
music.play()
pyglet.app.run()

I got this error:

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

subprocess also uses default player:

import subprocess

sound_program = "path to player"
sound_file = "D:/my path/rec1.mp3"
subprocess.call([sound_program, sound_file])

with mp3play, not sure how to use it:

import mp3play

filename = (r'D:\my path/rec1.mp3')
clip = mp3play.load(filename)
clip.play()

I tried it this way:

  filename = ('D:\my path/rec1.mp3')

this way:

  filename = r'D:\my path/rec1.mp3'

In all cases I got error:

Traceback (most recent call last):
  File "D:/dt/PyCharm_project/0_ASK.py", line 18, in <module>
    import mp3play
  File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\mp3play\__init__.py", line 4, in <module>
    from .windows import AudioClip as _PlatformSpecificAudioClip
  File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\mp3play\windows.py", line 27
    print 'Error %s for "%s": %s' % (str(err), txt, buf)
                                ^
SyntaxError: invalid syntax

ok so with pydub:

from pydub import AudioSegment 
from gtts import gTTS
import simpleaudio as sa

blabla = ('my voice')
tts = gTTS(text=blabla, lang='en')
tts.save("D:/my path/rec.mp3")

rec = AudioSegment.from_mp3("D:\my path\rec.mp3")
rec.export("rec.wav", format="wav")

#rec = AudioSegment.ffmpeg ("D:\my path\rec.mp3")
#rec.export("rec.wav", format="wav")

#rec = AudioSegment.converter ("D:\my path\rec.mp3")
#rec.export("rec.wav", format="wav")

wave_obj = sa.WaveObject.from_wave_file('D:/my path/rec.wav'')
play_obj = wave_obj.play()
play_obj.wait_done()

Errors in the sequence first with AudioSegment.from_mp3 :

RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File " D:/dt/PyCharm_project/0_ASK.py", line 9, in <module>
    rec = AudioSegment.from_mp3("D:\my path\rec.mp3")
  File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\audio_segment.py", line 438, in from_mp3
    return cls.from_file(file, 'mp3')
  File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\audio_segment.py", line 366, in from_file
    file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
  File "C:\Users\User\AppData\Roaming\Python\Python35\site-packages\pydub\utils.py", line 59, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
OSError: [Errno 22] Invalid argument: 'D:\my path\rec.mp3'

with AudioSegment.ffmpeg :

  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "D:/dt/PyCharm_project/0_ASK.py ", line 12, in <module>
    rec = AudioSegment.ffmpeg ("D:\my path\rec.mp3")
TypeError: 'str' object is not callable

with AudioSegment.converter:

RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "D:/dt/PyCharm_project/0_ASK.py", line 15, in <module>
    rec = AudioSegment.converter ("D:\my path\rec.mp3")
TypeError: 'str' object is not callable

not sure maybe webbrowser, but how to install it?

import webbrowser
webbrowser.open("D:/my path/rec1.mp3")

12条回答
ら.Afraid
2楼-- · 2019-04-07 03:01

Same problem here. It works with playsound 1.2.1 for me.

Install with :

$ pip install playsound

test with:

>>>from playsound import playsound
>>>playsound('/path/to/a/sound/file/you/want/to/play.mp3')
查看更多
够拽才男人
3楼-- · 2019-04-07 03:01

webbrowser should be part of you standard python install. Check if there is a webbrowser.py under C:\Your_Python_Folder\Lib.

Below code works just fine for me as webbrowser.py is present at above mentioned folder.

import webbrowser
webbrowser.open("rec.mp3")
查看更多
闹够了就滚
4楼-- · 2019-04-07 03:08

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.

查看更多
Bombasti
5楼-- · 2019-04-07 03:10

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
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-04-07 03:11

You can use playsound module

from playsound import playsound
playsound(file.mp3)

Boom that's it.it plays the audio right way works like charm

查看更多
时光不老,我们不散
7楼-- · 2019-04-07 03:11

If AVbin error occurs...

First download AVbin.exe and Install it then Go to local disk C --> windows --> System32 --> search the Avbin.dll and copy that file then paste that particular file in your root directory

Error will vanish

查看更多
登录 后发表回答