如何在 PyAudio 上实际播放歌曲?
我查看了这个问题: pyaudio help play a file
虽然这个问题确实得到了回答,但我从来没有得到关于实际将歌曲文件放在哪里的明确答案。
这是播放 WAVE 文件的代码:
""" Play a WAVE file. """
import pyaudio
import wave
import sys
chunk = 1024
if len(sys.argv) < 2:
print "Plays a wave file.\n\n" +\
"Usage: %s filename.wav" % sys.argv[0]
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data
data = wf.readframes(chunk)
# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
我浏览了代码,但在代码中找不到实际插入音乐文件本身的任何内容。当我按下程序中的“播放”按钮时(我在该程序中使用 wxform),什么也没有播放。
I looked at this question: pyaudio help play a file
While this question did get answered I never got a clear answer of where to actually put the song file.
This is the code for playing a WAVE file:
""" Play a WAVE file. """
import pyaudio
import wave
import sys
chunk = 1024
if len(sys.argv) < 2:
print "Plays a wave file.\n\n" +\
"Usage: %s filename.wav" % sys.argv[0]
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
# open stream
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data
data = wf.readframes(chunk)
# play stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
I've looked through the code but I can't find anything in the code where I actually insert the music file itself. When I press the "Play" button in my program (I use wxform with this program) nothing is played.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
神奇的线条是:
这似乎是说脚本的第一个参数 (
sys.argv[1]
) 用作waves 的输入。The magic line is:
This seems to say that the first argument to the script (
sys.argv[1]
) is used as the input for waves.我对 pyaudio 一无所知,但似乎很清楚歌曲文件是执行程序时传递给程序的第一个参数。看一下这一行:
wf = wave.open(sys.argv[1], 'rb')
只需将sys.arg[1]
更改为' c:/filename.wav'
之类的。如果您不向程序传递任何参数,程序将不会按照现在编写的方式运行。因为 if len(sys.argv)
2
块I don't know anything of pyaudio but it seems pretty clear that the song file is the first argument that is passed to the program when you execute it. Look att this line:
wf = wave.open(sys.argv[1], 'rb')
Just change tosys.arg[1]
to'c:/filename.wav'
or something.And the program won't run as it is written now if you don't pass any argument to it. Because of the
if len(sys.argv) < 2
block只了解 python、pyaudio 的一些知识,但似乎歌曲文件是执行程序时传递给程序的第一个参数。
只需更改插入这样的参数:
问候。
Just know a few things of python, pyaudio but it seems that the song file is the first argument that is passed to the program when you execute it.
Just change insert an argument like this :
regards.
这是一个解决方案:
注释掉If语句,直接添加要播放的文件名
Here is a solution :
Comment the If Statment and directly add the file name to play