pyaudio帮助播放文件 [英] pyaudio help play a file

查看:259
本文介绍了pyaudio帮助播放文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白pyaudio的例子材料。看来他们写了一个完整的小程序,它把我摔下。我怎么只打单一的音频文件?格式是不是一个问题,我只是想知道的最少code,我需要播放音频文件。

I did not understand the example material for pyaudio. It seems they had written an entire small program and it threw me off. How do I just play a single audio file? Format is not an issue, I just want to know the bare minimum code I need to play an audio file.

推荐答案

这个例子似乎pretty清晰。您只需例子保存为playwav.py电话:

The example seems pretty clear to me. You simply save the example as playwav.py call:

蟒蛇playwav.py my_fav_wav.wav

一些额外的意见波例如:

The wave example with some extra comments:

import pyaudio
import wave
import sys

# length of data to read.
chunk = 1024

# validation. If a wave file hasn't been specified, exit.
if len(sys.argv) < 2:
    print "Plays a wave file.\n\n" +\
          "Usage: %s filename.wav" % sys.argv[0]
    sys.exit(-1)

'''
************************************************************************
      This is the start of the "minimum needed to read a wave"
************************************************************************
'''
# open the file for reading.
wf = wave.open(sys.argv[1], 'rb')

# create an audio object
p = pyaudio.PyAudio()

# open stream based on the wave object which has been input.
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = wf.getframerate(),
                output = True)

# read data (based on the chunk size)
data = wf.readframes(chunk)

# play stream (looping from beginning of file to the end)
while data != '':
    # writing to the stream is what *actually* plays the sound.
    stream.write(data)
    data = wf.readframes(chunk)

# cleanup stuff.
stream.close()    
p.terminate()

这篇关于pyaudio帮助播放文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆