如何播放mp3 [英] how to play mp3
问题描述
我的问题从这里开始:
gTTS可以很好地工作,可以从文本文件中提取文本,但是首先要创建mp3文件,然后如果我想收听,我必须将其称为mp3,这样虽然很好,但是如果我可以避免使用任何音频文件并获得更好的效果,只是从文本文件中读取.也许我可以使用谷歌语音从文本文件中读取..?无论如何,现在的主要问题是其他
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
如果我只能使用gTTS,那么在Windows 10-64位Python 3.5上播放mp3的最佳方法是什么
if I can use only gTTS what is the best way to play mp3 on Windows 10-64 bit, Python 3.5
可以使用操作系统:
import os
os.startfile("D:\my path/rec1.mp3")
这很好,但是我不想使用默认播放器,需要mp3的simpleaudio之类的东西...
it is good, but I don't want use default player, need something like simpleaudio for mp3...
使用pygame时,我遇到安装问题,不确定如何以这种方式使用它:
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只是如何安装呢?与easy_install vlc
我得到错误:could not find suitable distribution for requirement.parse ('vlc')
和与pip install vlc
错误:could not find a version that satisfies the requirement vlc (from versions: ) no matching distribution found for vlc
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()
与pyglet:
import pyglet
music=pyglet.media.load('D:/my path/rec1.mp3')
music.play()
pyglet.app.run()
我收到此错误:
'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])
使用mp3play,不确定如何使用:
with mp3play, not sure how to use it:
import mp3play
filename = (r'D:\my path/rec1.mp3')
clip = mp3play.load(filename)
clip.play()
我这样尝试过:
filename = ('D:\my path/rec1.mp3')
这种方式:
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
好吧,使用pydub:
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()
先出现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'
和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
与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
不确定Webbrowser,但是如何安装?
not sure maybe webbrowser, but how to install it?
import webbrowser
webbrowser.open("D:/my path/rec1.mp3")
推荐答案
此处存在相同问题.对我来说,它可以与 playsound 1.2.1 一起使用.
Same problem here. It works with playsound 1.2.1 for me.
安装:
$ pip install playsound
测试:
>>>from playsound import playsound
>>>playsound('/path/to/a/sound/file/you/want/to/play.mp3')
这篇关于如何播放mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!