Play a Sound with Python

What's the easiest way to play a sound file (.wav) in Python? By easiest I mean both most platform independent and requiring the least dependencies. pygame is certainly an option, but it seems overkill for just sound.

472270 次浏览

PyMedia 的声音示例执行 仅此而已

import time, wave, pymedia.audio.sound as sound
f= wave.open( 'YOUR FILE NAME', 'rb' )
sampleRate= f.getframerate()
channels= f.getnchannels()
format= sound.AFMT_S16_LE
snd= sound.Output( sampleRate, channels, format )
s= f.readframes( 300000 )
snd.play( s )

The Snack Sound Toolkit can play wav, au and mp3 files.

s = Sound()
s.read('sound.wav')
s.play()

Definitely use 小猪 for this. It's kind of a large package, but it is pure python with no extension modules. That will definitely be the easiest for deployment. It's also got great format and codec support.

import pyglet


music = pyglet.resource.media('music.mp3')
music.play()


pyglet.app.run()

对于 Windows,你可以使用 winsound,它是内置的

import winsound


winsound.PlaySound('sound.wav', winsound.SND_FILENAME)

你应该可以在 linux 上使用 ossaudiodev:

from wave import open as waveOpen
from ossaudiodev import open as ossOpen
s = waveOpen('tada.wav','rb')
(nc,sw,fr,nf,comptype, compname) = s.getparams( )
dsp = ossOpen('/dev/dsp','w')
try:
from ossaudiodev import AFMT_S16_NE
except ImportError:
from sys import byteorder
if byteorder == "little":
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
else:
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
dsp.setparameters(AFMT_S16_NE, nc, fr)
data = s.readframes(nf)
s.close()
dsp.write(data)
dsp.close()

(来自 ossaudiodev: Bill Dandreta http://mail.python.org/pipermail/python-list/2004-October/288905.html)

我喜欢 pygame,下面的命令应该可以起作用:

pygame.init()
pygame.mixer.Sound('sound.wav').play()

但是我的电脑上都没有,而且这方面的帮助有限。编辑: 我知道为什么 pygame 的声音对我不起作用了,它没有正确加载大多数声音,当我加载它们时,‘ length’属性是 ~ 0.0002。也许下载他们使用其他东西,而不是我的游戏将得到它更普遍的马克。

with pyglet I'm getting a resource not found error Using the above example, wigh both relative and full paths to the files.

使用 pyglet.media.load()而不是 pyglet.resource.media()让我加载文件。

but sound.play() only plays the first fraction of a second of the file, unless I run pyglet.app.run() which blocks everything else...

wxPython has support for playing wav files on Windows and Unix - I am not sure if this includes Macs. However it only support wav files as far as I can tell - it does not support other common formats such as mp3 or ogg.

在 play ()命令添加10秒左右的延迟之后,它就可以工作了

import pygame


import time


pygame.init()


pygame.mixer.music.load("test.wav")


pygame.mixer.music.play()


time.sleep(10)

这也可以播放.mp3文件。

我刚刚在 sox 上发布了一个简单的 Python 包装器,它可以用 Python 播放声音。它非常容易安装,因为您需要 Python 2.6或更高版本、 sox (对于大多数架构来说,很容易获得二进制文件)和包装器(https://github.com/standarddeviant/sound4python)。如果你没有红袜队比赛,请点击这里: http://sourceforge.net/projects/sox/files/sox/

你可以通过以下方式播放音频:

from sound4python import sound
import random
a = []
for idx in xrange(1*16000):
a.append(random.randint(-16384,16384))
sound(a)

请记住,实际上播放音频的部分只有以下几个:

from sound4python import sound
...
sound(a)

对于 Linux 用户,如果需要低级的 pcm 数据操作,请尝试 阿尔萨奥迪奥模块。在包中还有一个 playwave. py 示例。

这似乎荒谬和牵强附会,但你总是可以使用 Windows (或任何操作系统,你喜欢)为你管理的声音!

import os
os.system("start C:/thepathyouwant/file")

简单,没有扩展,有点慢和垃圾,但工作。