播放wav文件python 3 [英] Play wav file python 3

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

问题描述

我想在Python 3.4中播放.wav文件.另外,我希望python播放文件而不是python打开文件以在VLC,媒体播放器等中播放.

I want to play a .wav file in Python 3.4. Additonally, I want python to play the file rather than python open the file to play in VLC, media player etc..

作为后续问题,我有什么办法可以将.wav文件和.py文件组合成一个独立的exe.

As a follow up question, is there any way for me to combine the .wav file and the .py file into a standalone exe.

如果它是愚蠢的,请忽略问题的第二部分,我对编译python一无所知.

Ignore the second part of the question if it is stupid, I don't really know anything about compiling python.

此外,我知道关于.wav文件还有其他问题,但是我还没有找到按照我所描述的方式在python 3.4中工作的问题.

Also, I know there have been other questions about .wav files, but I have not found one that works in python 3.4 in the way I described.

推荐答案

我通过使用模块pyaudio解决了该问题,并使用wave模块读取了文件. 我将键入示例代码来播放一个简单的wave文件.

I fixed the problem by using the module pyaudio, and the module wave to read the file. I will type example code to play a simple wave file.

import wave, sys, pyaudio
wf = wave.open('Sound1.wav')
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = wf.getframerate(),
                output = True)
data = wf.readframes(chunk)
while data != '':
    stream.write(data)
    data = wf.readframes(chunk)

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

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