Python-对winsound使用多个标志吗? [英] Python - Using multiple flags for winsound?
问题描述
这是我当前的代码:
import winsound as wav
wav.PlaySound("music.wav", wav.SND_LOOP | wav.SND_ASYNC)
input()
wav.PlaySound("beep.wav", wav.SND_ASYNC | wav.SND_NOSTOP)
从 python winsound
文档:" [文件的[Winsound]的解释取决于标志的值,标志的值可以是下面描述的常量的按位或"组合"
音乐本身异步播放并循环播放.但是,当上面的代码发出提示音时,它将引发错误(sprites.py是代码的文件):
The music itself plays asynchronously, and loops. However, when the above code plays the beep, it throws an error (sprites.py is the code's file):
Traceback (most recent call last):
File ".../sprites.py", line 5, in <module>
wav.PlaySound("beep.wav", wav.SND_ASYNC | wav.SND_NOSTOP)
RuntimeError: Failed to play sound
交换 SND_ASYNC
和 SND_NOSTOP
会产生相同的错误,并且删除 SND_NOSTOP
会导致发出哔声,但音乐会中断.
Swapping SND_ASYNC
and SND_NOSTOP
produces an identical error, and removing the SND_NOSTOP
results in the beep playing, but the music cuts out.
为什么会发生这种情况,并且如果无法解决,还有其他方法可以使蜂鸣声播放而不会中断音乐吗?
Why is this happening, and if it's unfixable, is there another way I can cause the beep to play without cutting off the music?
如果重要的话,声音文件位于此处.
The Sound files are here, if that's important.
这个问题存在,但是没有出现有答案.
This question exists, however it does not appear to have an answer.
推荐答案
您正在整理标志就好了;那不是问题.问题在于您使用的API不支持一次播放多个声音.
You are combing flags just fine; that's not the issue. The problem is that the API you are using doesn't support playing more than one sound at a time.
换句话说,抛出异常是因为已经循环播放了声音. SND_NOSTOP
标志仅在您要防止当前播放的声音被打断时使用;出现异常表示当前正在播放声音.如果没有该标志,则旧声音停止并播放新声音.
In other words, the exception is thrown because there is already a sound being played, in a loop. The SND_NOSTOP
flag is only of use when you want to prevent a currently playing sound from being interrupted; the exception is raised to indicate that sound is playing right now. Without the flag the old sound is stopped and the new sound is played.
The winsound
Python module is little more than a thin wrapper around the Windows API PlaySound
function, which states:
SND_NOSTOP
指定的声音事件将产生另一个已经在同一进程中播放的声音事件.如果由于生成该声音所需的资源正忙于播放另一种声音而无法播放声音,则该函数会立即返回 FALSE (不假),而不会播放请求的声音.
SND_NOSTOP
The specified sound event will yield to another sound event that is already playing in the same process. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
如果未指定此标志,则PlaySound尝试停止在同一进程中当前正在播放的任何声音.在其他过程中播放的声音不受影响.
If this flag is not specified, PlaySound attempts to stop any sound that is currently playing in the same process. Sounds played in other processes are not affected.
仅当当前没有其他声音在播放时,才使用该标志乐观地播放声音:
Use the flag to optimistically play sounds only if nothing else is currently playing:
try:
wav.PlaySound("beep.wav", wav.SND_ASYNC | wav.SND_NOSTOP)
except RuntimeError:
# sound api busy with another sound, tough luck
pass
要同时播放声音,您需要一个可以混合声音的API. PlaySound
无法做到这一点,并且标准库中没有其他可以做到的.
In order to play sounds simultaneously you need an API that can mix sounds; PlaySound
can't do this, and there is nothing else in the standard library that can.
除了让您使用DirectSound API之外,还需要一些东西.类似于 pyglet库.该库包含出色的音频API ,使其易于组合循环播放音乐,并偶尔播放其他声音效果.
You need something than lets you use the DirectSound API instead. Like the pyglet library. This library includes an excellent audio API that should make it trivial to combine looped music with occasional other sound effects playing in parallel.
这篇关于Python-对winsound使用多个标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!