34.4. winsound — Windows 声音播放接口


winsound 模块提供对由 Windows 平台提供的基本声音播放机制的访问。它包括一些函数和几个常量。

winsound. Beep ( frequency , duration )

哔哔 PC 扬声器。 frequency 参数指定声音的频率 (以赫兹为单位),且必须在 37 到 32,767 范围内。 duration 参数指定声音应该持续的毫秒数。若系统不能哔哔扬声器, RuntimeError 被引发。

winsound. PlaySound ( sound , flags )

调用底层 PlaySound() 函数从平台 API。 sound 参数可以是文件名、系统声音别名、音频数据如 像字节对象 ,或 None . Its interpretation depends on the value of flags , which can be a bitwise ORed combination of the constants described below. If the sound 参数为 None , any currently playing waveform sound is stopped. If the system indicates an error, RuntimeError 被引发。

winsound. MessageBeep ( type=MB_OK )

调用底层 MessageBeep() 函数从平台 API。这播放如注册表中的指定声音。 type 自变量指定要播放的声音;可能的值是 -1 , MB_ICONASTERISK , MB_ICONEXCLAMATION , MB_ICONHAND , MB_ICONQUESTION ,和 MB_OK ,所有描述见下文。值 -1 produces a “simple beep”; this is the final fallback if a sound cannot be played otherwise. If the system indicates an error, RuntimeError 被引发。

winsound. SND_FILENAME

sound 参数是 WAV 文件的名称。不要使用采用 SND_ALIAS .

winsound. SND_ALIAS

sound parameter is a sound association name from the registry. If the registry contains no such name, play the system default sound unless SND_NODEFAULT is also specified. If no default sound is registered, raise RuntimeError . Do not use with SND_FILENAME .

所有 Win32 系统至少支持以下;大多数系统支持更多:

PlaySound() name 相应的控制面板声音名称
'SystemAsterisk' 星号
'SystemExclamation' 感叹号
'SystemExit' 退出 Windows
'SystemHand' 严重错误停止
'SystemQuestion' 询问

例如:

import winsound
# Play Windows exit sound.
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
# Probably play Windows default sound, if any is registered (because
# "*" probably isn't the registered name of any sound).
winsound.PlaySound("*", winsound.SND_ALIAS)
									
winsound. SND_LOOP

重复播放声音。 SND_ASYNC flag must also be used to avoid blocking. Cannot be used with SND_MEMORY .

winsound. SND_MEMORY

sound parameter to PlaySound() is a memory image of a WAV file, as a 像字节对象 .

注意

This module does not support playing from a memory image asynchronously, so a combination of this flag and SND_ASYNC 会引发 RuntimeError .

winsound. SND_PURGE

停止播放指定声音的所有实例。

注意

现代 Windows 平台不支持此标志。

winsound. SND_ASYNC

立即返回,允许异步播放声音。

winsound. SND_NODEFAULT

若找不到指定声音,不播放系统默认声音。

winsound. SND_NOSTOP

不中断目前播放的声音。

winsound. SND_NOWAIT

立即返回,若声音驱动程序繁忙。

注意

现代 Windows 平台不支持此标志。

winsound. MB_ICONASTERISK

播放 SystemDefault 声音。

winsound. MB_ICONEXCLAMATION

播放 SystemExclamation 声音。

winsound. MB_ICONHAND

播放 SystemHand 声音。

winsound. MB_ICONQUESTION

播放 SystemQuestion 声音。

winsound. MB_OK

播放 SystemDefault 声音。

上一话题

34.3. winreg — Windows 注册表访问

下一话题

35. Unix 特定服务

本页