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

时间:2021-07-17 02:15:43

I tried playing a .wav file using pyaudio. It works great on windows, but doesn't work in Ubuntu when another device is using sound.

我尝试使用pyaudio播放.wav文件。它在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?

我可以尝试使用另一个库吗?另一种方法?

5 个解决方案

#1


12  

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演示的一个例子。

#2


4  

Have you looked at pymedia? It looks as easy as this to play a WAV file:

你看过pymedia吗?播放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

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. 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.

最后,虽然我不知道pyaudio的局限性,但您的错误消息听起来更像是库无法找到默认输出设备而不是另一个进程正在使用该设备。也许看一下pyaudio的get_default_output_device_info返回的输出设备,并将它与Ubuntu中的默认设置进行比较。

#3


2  

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

#4


1  

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

from pygame import mixer

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

#5


0  

I found playsound to be the simplest.

我发现playound是最简单的。

from playsound import playsound

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

#1


12  

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演示的一个例子。

#2


4  

Have you looked at pymedia? It looks as easy as this to play a WAV file:

你看过pymedia吗?播放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

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. 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.

最后,虽然我不知道pyaudio的局限性,但您的错误消息听起来更像是库无法找到默认输出设备而不是另一个进程正在使用该设备。也许看一下pyaudio的get_default_output_device_info返回的输出设备,并将它与Ubuntu中的默认设置进行比较。

#3


2  

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

#4


1  

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

from pygame import mixer

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

#5


0  

I found playsound to be the simplest.

我发现playound是最简单的。

from playsound import playsound

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