使用 Python 播放声音

发布于 2024-07-08 21:46:18 字数 101 浏览 7 评论 0原文

在 Python 中播放声音文件 (.wav) 最简单的方法是什么? 我所说的最简单是指最独立于平台并且需要最少的依赖性。 pygame 当然是一个选择,但对于声音来说似乎有点过分了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

迟到的我 2024-07-15 21:46:18

对于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)

For Windows, you can use winsound. It's built in

import winsound

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

You should be able to use ossaudiodev for linux:

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()

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

世界等同你 2024-07-15 21:46:18

这看起来很荒谬且牵强,但您始终可以使用 Windows(或您喜欢的任何操作系统)来为您管理声音!

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

简单,没有扩展,有点慢而且垃圾,但是可以工作。

This seems ridiculous and far fetched but you could always use Windows (or whatever OS you prefer) to manage the sound for you!

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

Simple, no extensions, somewhat slow and junky, but working.

计㈡愣 2024-07-15 21:46:18

Snack Sound Toolkit 可以播放 wav、au 和 mp3 文件。

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

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

s = Sound() 
s.read('sound.wav') 
s.play()
吃→可爱长大的 2024-07-15 21:46:18

为此,请务必使用 Pyglet 。 这是一个很大的包,但它是纯Python,没有扩展模块。 这绝对是最容易部署的。 它还具有出色的格式和编解码器支持。

import pyglet

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

pyglet.app.run()

Definitely use Pyglet 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()
几味少女 2024-07-15 21:46:18

在 play() 命令添加大约 10 秒左右的延迟后,它将起作用。

import pygame

import time

pygame.init()

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

pygame.mixer.music.play()

time.sleep(10)

这也可以播放 .mp3 文件。

After the play() command add a delay of say 10 secs or so, it'll work

import pygame

import time

pygame.init()

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

pygame.mixer.music.play()

time.sleep(10)

This also plays .mp3 files.

远山浅 2024-07-15 21:46:18

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 )

pyMedia's sound example does just that. This should be all you need.

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 )
归途 2024-07-15 21:46:18

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

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

但它在我的任何一台计算机上都不起作用,并且关于该主题的帮助有限。 编辑:我弄清楚为什么 pygame 声音对我不起作用,它没有正确加载大多数声音,当我加载它们时,“长度”属性是〜0.0002。 也许使用 mygame 以外的其他东西加载它们会让它更普遍地停止。

使用 pyglet 我收到资源未找到错误使用上面的示例,查看文件的相对路径和完整路径。

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

但是 sound.play() 只播放文件的第一部分,除非我运行 pyglet.app.run() ,它会阻止其他所有内容......

I like pygame, and the command below should work:

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

but it doesn't on either of my computers, and there is limited help on the subject out there. edit: I figured out why the pygame sound isn't working for me, it's not loading most sounds correctly, the 'length' attribute is ~0.0002 when I load them. maybe loading them using something other than mygame will get it morking more generally.

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

using pyglet.media.load() instead of pyglet.resource.media() lets me load the files.

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

ゝ偶尔ゞ 2024-07-15 21:46:18

wxPython 支持在 Windows 和 Unix 上播放 wav 文件 - 我不确定这是否包括 Mac。 然而,据我所知,它只支持 wav 文件 - 它不支持其他常见格式,例如 mp3 或 ogg。

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.

二智少女猫性小仙女 2024-07-15 21:46:18

我刚刚发布了一个简单的 sox 周围的 python 包装器,它将用 Python 播放声音。 安装非常简单,因为您需要 Python 2.6 或更高版本、sox(可以轻松获取适用于大多数架构的二进制文件)和包装器 ( https://github.com/standarddeviant/sound4python)。 如果您没有 sox,请转到此处: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)    

I just released a simple python wrapper around sox that will play a sound with Python. It's very easy to install as you need Python 2.6 or greater, sox (easy to get binaries for most architectures) and the wrapper ( https://github.com/standarddeviant/sound4python ). If you don't have sox, go here: http://sourceforge.net/projects/sox/files/sox/

You would play audio with it by:

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

Keep in mind, the only parts actually involved in playing audio are just these:

from sound4python import sound
...
sound(a)    
四叶草在未来唯美盛开 2024-07-15 21:46:18

对于 Linux 用户,如果需要低级 pcm 数据操作,请尝试 alsaaudio 模块。 包内也有一个 playwav.py 示例。

For Linux user, if low level pcm data manipulation is needed, try alsaaudio module. There is a playwav.py example inside the package too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文