使用ossaudiodev进行播放
我正在尝试编写一个流音频客户端,我想使用 ossaudiodev 函数来帮助我在本地计算机上播放音频。不幸的是,每次尝试运行代码时我都会遇到异常:
Unhandled exception in thread started by <function audioplayer at 0x88e96f4>
Traceback (most recent call last):
File "client.py", line 56, in audioplayer
audio = ossaudiodev.open("/dev/audio", 'w')
IOError: [Errno 16] Device or resource busy: '/dev/audio'
这是我的代码。我认为很简单,但不确定如何释放 /dev/audio 资源。
def audioplayer():
audio = ossaudiodev.open("/dev/audio", 'w')
audio.setfmt(ossaudiodev.AFMT_MU_LAW)
audio.channels(2)
audio.speed(8000)
packet = 0
sleep(.20) # give it a little bit of time to fill ze buffers
while (packets[packet] != "\0"):
audio.write(packets[packet])
packet += 1
这有什么技巧吗?
编辑:答案是显而易见且愚蠢的。我在运行代码时正在玩潘多拉,因此扬声器“忙着”这样做。谁能澄清为什么会这样吗?其他应用程序如何同时运行多个音频流,而我似乎不能?
I'm trying to write a streaming audio client and I'd like to use ossaudiodev functions to help me play back the audio on my local machine. Unfortunately, I'm getting an exception every time I try to run my code:
Unhandled exception in thread started by <function audioplayer at 0x88e96f4>
Traceback (most recent call last):
File "client.py", line 56, in audioplayer
audio = ossaudiodev.open("/dev/audio", 'w')
IOError: [Errno 16] Device or resource busy: '/dev/audio'
Here's my code. Pretty simple methinks, but not sure how to free up the /dev/audio resource.
def audioplayer():
audio = ossaudiodev.open("/dev/audio", 'w')
audio.setfmt(ossaudiodev.AFMT_MU_LAW)
audio.channels(2)
audio.speed(8000)
packet = 0
sleep(.20) # give it a little bit of time to fill ze buffers
while (packets[packet] != "\0"):
audio.write(packets[packet])
packet += 1
Any tricks to this?
Edit: The answer is obvious and stupid. I was playing pandora while running the code, thus the speakers were "busy" doing that. Can anyone clarify why this is so? How can other applications all run multiple audio streams at the same time, yet I seemingly can't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使在可用的系统上,OSS 接口通常也只是 ALSA 的一个外观。考虑使用支持多个接口的库,例如 pyao。
Even on systems where it's available, the OSS interface is usually just a facade over ALSA. Consider using a library that supports multiple interfaces, such as pyao.
你的桌面管理器已经在使用你的声卡,你可能会更幸运地使用 alsa 的 dmix 插件,但是
Soundblaster live 具有硬件混合功能通常很困难,因此你可以多次打开它
,是的,你肯定可以找到 pyao 的工作后端
your desktop manager is already using your soundcard, you may have more luck with dmix plugin for alsa, but it's often difficult
soundblaster live has hw mixing so you can open it multiple times
and yes definitively you can find a working backend of pyao