简体   繁体   English

什么是在python中播放声音文件的跨平台方式?

[英]What's a cross platform way to play a sound file in python?

I tried playing a .wav file using pyaudio. 我尝试使用pyaudio播放.wav文件。 It works great on windows, but doesn't work in Ubuntu when another device is using sound. 它在Windows上运行良好,但在其他设备使用声音时在Ubuntu中不起作用。

The error is "IOError: [Errorno Invalid output device (no default output device)] -9996 错误是“IOError:[Errorno无效的输出设备(无默认输出设备)] -9996

Is there another library I could try to use? 我可以尝试使用另一个库吗? Another method? 另一种方法?

You can use wxPython 你可以使用wxPython

sound = wx.Sound('sound.wav')
sound.Play(wx.SOUND_SYNC)

or 要么

sound.Play(wx.SOUND_ASYNC)

Here is an example from the wxPython demo. 以下是wxPython演示中的示例。

Have you looked at pymedia? 你看过pymedia吗? It looks as easy as this to play a WAV file: 播放WAV文件看起来很简单:

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)
while snd.isPlaying(): time.sleep(0.05)

Ref: http://pymedia.org/tut/play_wav.html 参考: http//pymedia.org/tut/play_wav.html

Of course, you can have a look at the Python wiki under Audio ( http://wiki.python.org/moin/Audio/ ) for other libraries such as https://docs.python.org/library/wave.html or again in Python's wiki under Game Libraries ( http://wiki.python.org/moin/PythonGameLibraries ) that will point you to bindings to OpenAL or Pygame that has sound modules. 当然,您可以查看Audio( http://wiki.python.org/moin/Audio/ )下的Python wiki,了解其他库,例如https://docs.python.org/library/wave.html或者再次在Python的游戏库( http://wiki.python.org/moin/PythonGameLibraries )下的wiki中,它将指向绑定到具有声音模块的OpenAL或Pygame。

And finally, although I don't know the limitations of pyaudio, your error message sounds more like the library is not able to find the default output device more than the device is in use by another process. 最后,虽然我不知道pyaudio的局限性,但是您的错误消息听起来更像是库无法找到默认输出设备而不是另一个进程正在使用该设备。 Maybe have a look at what output device is returned by the get_default_output_device_info of pyaudio and compare it to whatever's your default setting in Ubuntu. 也许看一下get_default_output_device_info的get_default_output_device_info返回的输出设备,并将它与Ubuntu中的默认设置进行比较。

You can try Simpleaudio : 您可以尝试Simpleaudio

> pip install simpleaudio

Then: 然后:

import simpleaudio as sa

wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
play_obj = wave_obj.play()
play_obj.wait_done()

I'm not absolutely sure if that fulfills your requirements, but I immediately thought PyGame 我不确定这是否符合你的要求,但我立刻想到了PyGame

http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound

from pygame import mixer

mixer.init()
s = mixer.Sound('sound.wav')
s.play()

I found playsound to be the simplest. 我发现playsound是最简单的。

from playsound import playsound

is_synchronus = False
playsound(r"C:\Windows\Media\chimes.wav", is_synchronus)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM