pygame.mixer
pygame module for loading and playing sounds
— initialize the mixer module
— preset the mixer init arguments
— uninitialize the mixer
— test if the mixer is initialized
— stop playback of all sound channels
— temporarily stop playback of all sound channels
— resume paused playback of sound channels
— fade out the volume on all sounds before stopping
— set the total number of playback channels
— get the total number of playback channels
— reserve channels from being automatically used
— find an unused channel
— test if any sound is being mixed
— get the mixer's SDL version
— Create a new Sound object from a file or buffer object
— Create a Channel object for controlling playback

此模块包含用于加载Sound对象和控制播放的类。MIXER模块是可选的,取决于SDL_MIXER。您的程序应该测试这一点 pygame.mixerpygame module for loading and playing sounds 是可用的,并且在使用它之前进行了初始化。

混音器模块用于播放声音的声道数量有限。通常,程序会告诉PYGAME开始播放音频,它会自动选择一个可用的频道。默认为8个同时使用的频道,但复杂的程序可以更精确地控制频道的数量及其使用。

所有声音播放都混合在后台线程中。当您开始播放Sound对象时,它将立即返回,同时声音继续播放。还可以多次主动播放单个Sound对象。

混音器也有一个特殊的流媒体频道。这是用于音乐播放的,可通过 pygame.mixer.musicpygame module for controlling streamed audio 模块。考虑使用此模块播放长期播放的音乐。与混音器模块不同,音乐模块不需要立即将音乐加载到内存中,而是将文件中的音乐串流。

混合器模块必须像其他pyGame模块一样进行初始化,但它有一些额外的条件。这个 pygame.mixer.init() 函数使用几个可选参数来控制回放速率和样本大小。PYGAME将默认为合理的值,但是PYGAME不能执行声音重采样,因此混音器应该被初始化以匹配音频资源的值。

NOTE: For less laggy sound use a smaller buffer size. The default is set to reduce the chance of scratchy sounds on some computers. You can change the default buffer by calling pygame.mixer.pre_init()preset the mixer init arguments before pygame.mixer.init()initialize the mixer module or pygame.init()initialize all imported pygame modules is called. For example: pygame.mixer.pre_init(44100,-16,2, 1024)

pygame.mixer.init()
initialize the mixer module
init(frequency=44100, size=-16, channels=2, buffer=512, devicename=None, allowedchanges=AUDIO_ALLOW_FREQUENCY_CHANGE | AUDIO_ALLOW_CHANNELS_CHANGE) -> None

初始化混音器模块以加载和回放声音。可以覆盖默认参数以提供特定的音频混合。接受关键字参数。为了向后兼容,参数值0将替换为启动缺省值, allowedchanges ,其中使用-1。(启动默认设置可以通过 pre_init() 呼叫)。

Size参数表示每个音频样本使用的位数。如果该值为负,则将使用带符号的样本值。正值表示将使用无符号音频样本。无效值将引发异常。

Channels参数用于指定是使用单声道还是立体声。单声道为1,立体声为2。

Buffer参数控制混音器中使用的内部采样数。缺省值应该适用于大多数情况。可以降低它以减少延迟,但可能会发生声音丢失。可以将其提高到更大的值,以确保播放永远不会跳过,但它会对声音播放造成延迟。缓冲区大小必须是2的幂(如果不是,则向上舍入到下一个最接近的2的幂)。

某些平台要求 pygame.mixerpygame module for loading and playing sounds 在显示模块初始化后要初始化的模块。最高级别 pygame.init() 会自动处理这一点,但不能将任何参数传递给混合器init。为了解决这个问题,Mixer有一个功能 pygame.mixer.pre_init() 在使用TopLevel init之前设置适当的默认值。

当使用AllowedChanges=0时,它将在运行时转换样本以匹配硬件支持的内容。例如,声卡可能不支持16位声音样本,因此它将在内部使用8位样本。如果提供了AUDIO_ALLOW_FORMAT_CHANGE,则请求的格式将更改为SDL2支持的最接近格式。

除0之外,AllowedChanged还接受以下一起或的常量:

  • AUDIO_ALLOW_FREQUENCY_CHANGE

  • AUDIO_ALLOW_FORMAT_CHANGE

  • AUDIO_ALLOW_CHANNELS_CHANGE

  • AUDIO_ALLOW_ANY_CHANGE

多次调用此函数是安全的,但在混合器初始化后,如果不先调用,则无法更改回放参数 pygame.mixer.quit() 。

Changed in pygame 1.8: 默认设置 buffersize 从1024更改为3072。

Changed in pygame 1.9.1: 默认设置 buffersize 从3072改为4096。

Changed in pygame 2.0.0: 默认设置 buffersize 从4096改为512。

Changed in pygame 2.0.0: 默认设置 frequency 从22050更改为44100。

Changed in pygame 2.0.0: size 可以是32(32位浮点数)。

Changed in pygame 2.0.0: channels 也可以是4或6。

New in pygame 2.0.0: allowedchanges , devicename 添加的参数

pygame.mixer.pre_init()
preset the mixer init arguments
pre_init(frequency=44100, size=-16, channels=2, buffer=512, devicename=None, allowedchanges=AUDIO_ALLOW_FREQUENCY_CHANGE | AUDIO_ALLOW_CHANNELS_CHANGE) -> None

调用pre_init以更改在调用REAL时使用的默认值 pygame.mixer.init() 叫做。接受关键字参数。设置自定义混音器回放值的最佳方法是调用 pygame.mixer.pre_init() 在调用顶层之前 pygame.init() 。为了向后兼容,参数值0将替换为启动缺省值, allowedchanges ,其中使用-1。

Changed in pygame 1.8: 默认设置 buffersize 从1024更改为3072。

Changed in pygame 1.9.1: 默认设置 buffersize 从3072改为4096。

Changed in pygame 2.0.0: 默认设置 buffersize 从4096改为512。

Changed in pygame 2.0.0: 默认设置 frequency 从22050更改为44100。

New in pygame 2.0.0: allowedchanges , devicename 添加的参数

pygame.mixer.quit()
uninitialize the mixer
quit() -> None

这将取消初始化 pygame.mixerpygame module for loading and playing sounds 。如果稍后重新初始化混音器,则所有播放将停止,并且任何加载的Sound对象可能与混音器不兼容。

pygame.mixer.get_init()
test if the mixer is initialized
get_init() -> (frequency, format, channels)

如果混合器已初始化,则返回它正在使用的回放参数。如果混合器尚未初始化,则返回 None 。

pygame.mixer.stop()
stop playback of all sound channels
stop() -> None

这将停止所有活动混音器频道的所有播放。

pygame.mixer.pause()
temporarily stop playback of all sound channels
pause() -> None

这将暂时停止活动混音器通道上的所有播放。稍后可以使用以下命令继续播放 pygame.mixer.unpause()

pygame.mixer.unpause()
resume paused playback of sound channels
unpause() -> None

这将在暂停之后恢复所有活动的声道。

pygame.mixer.fadeout()
fade out the volume on all sounds before stopping
fadeout(time) -> None

这将以毫秒为单位淡出所有活动通道上的音量。声音静音后,播放将停止。

pygame.mixer.set_num_channels()
set the total number of playback channels
set_num_channels(count) -> None

设置混音器的可用通道数。默认值为8。该值可以增加或减少。如果该值减小,则在截断的通道上播放的声音将停止。

pygame.mixer.get_num_channels()
get the total number of playback channels
get_num_channels() -> count

返回当前活动的播放频道数。

pygame.mixer.set_reserved()
reserve channels from being automatically used
set_reserved(count) -> count

混音器可以保留任何数量的频道,这些频道不会被自动选择用于声音播放。如果当前正在保留的频道上播放声音,则不会停止播放。

这允许应用程序为不能丢弃的重要声音预留特定数量的频道,或者保留有保证的频道来播放。

将返回实际保留的频道数,这可能少于请求的频道数,具体取决于先前分配的频道数。

pygame.mixer.find_channel()
find an unused channel
find_channel(force=False) -> Channel

这将查找并返回非活动的Channel对象。如果没有非活动通道,则此函数将返回 None 。如果没有非活动通道,并且force参数为 True ,这将找到声音运行时间最长的频道并返回它。

pygame.mixer.get_busy()
test if any sound is being mixed
get_busy() -> bool

退货 True 如果混音器忙于混音任何声道。如果混合器空闲,则此返回 False 。

pygame.mixer.get_sdl_mixer_version()
get the mixer's SDL version
get_sdl_mixer_version() -> (major, minor, patch)
get_sdl_mixer_version(linked=True) -> (major, minor, patch)
参数

linked (bool) -- 如果 True (缺省)返回链接的版本号,否则返回编译的版本号

返回

混音器的SDL库版本号(根据 linked 参数)作为3个整数的元组 (major, minor, patch)

返回类型

tuple

备注

链接的版本号和编译的版本号应该相同。

New in pygame 2.0.0.

pygame.mixer.Sound
Create a new Sound object from a file or buffer object
Sound(filename) -> Sound
Sound(file=filename) -> Sound
Sound(file=pathlib_path) -> Sound
Sound(buffer) -> Sound
Sound(buffer=buffer) -> Sound
Sound(object) -> Sound
Sound(file=object) -> Sound
Sound(array=object) -> Sound
— begin sound playback
— stop sound playback
— stop sound playback after fading out
— set the playback volume for this Sound
— get the playback volume
— count how many times this Sound is playing
— get the length of the Sound
— return a bytestring copy of the Sound samples.

从文件名、Python文件对象或可读缓冲区对象加载新的声音缓冲区。将执行有限的重采样,以帮助样本匹配混合器的初始化参数。Unicode字符串只能是文件路径名。字节对象可以是路径名,也可以是缓冲区对象。使用‘FILE’或‘BUFFER’关键字以避免歧义;否则Sound可能会猜错。如果使用了ARRAY关键字,则预计该对象将导出新的缓冲区接口(首先检查该对象的缓冲区接口)。

Sound对象表示实际的声音样本数据。更改Sound对象状态的方法将更改Sound播放的所有实例。Sound对象还会导出新的缓冲区接口。

Sound可以从 OGG 音频文件或来自未压缩的 WAV 。

注意:缓冲区将在内部复制,它与Sound对象之间不会共享任何数据。

目前,缓冲区和数组支持与 sndarray.make_sound 对于数值数组,该示例符号和字节顺序将被忽略。这将通过正确处理符号和字节顺序或在不同时引发异常来更改。此外,源样本被截断以适应音频样本大小。这一点不会改变。

New in pygame 1.8: pygame.mixer.Sound(buffer)

New in pygame 1.9.2: pygame.mixer.SoundCreate a new Sound object from a file or buffer object 关键字参数和数组接口支持

New in pygame 2.0.1: 路径库.对Python3的路径支持。

play()
begin sound playback
play(loops=0, maxtime=0, fade_ms=0) -> Channel

开始在可用的频道上播放声音(即,在计算机的扬声器上)。这将强制选择一个频道,因此如果需要,播放可能会切断当前正在播放的声音。

LOOPS参数控制样本在第一次播放后将重复播放的次数。值为5表示声音将播放一次,然后重复五次,因此总共播放六次。默认值(零)表示声音不会重复,因此仅播放一次。如果将-1f25 LOOPS-1f6设置为-1f25 Sound-1f6(声音)无限期循环(尽管您仍然可以调用 stop() 来阻止它)。

Maxtime参数可用于在给定的毫秒数后停止播放。

Fade_ms参数将使声音以0音量开始播放,并在给定的时间内逐渐减弱到最大音量。样本可能会在淡入完成之前结束。

这将返回所选频道的Channel对象。

stop()
stop sound playback
stop() -> None

这将停止在任何活动通道上播放此声音。

fadeout()
stop sound playback after fading out
fadeout(time) -> None

这将在以毫秒为单位的时间参数中淡出声音后停止播放声音。声音将在所有正在播放的频道上淡出并停止。

set_volume()
set the playback volume for this Sound
set_volume(value) -> None

这将设置此声音的播放音量(响度)。如果声音正在播放,这将立即影响声音。它还将影响将来播放此声音的任何内容。

参数

value (float) -- 音量在0.0到1.0之间(含)|如果值<0.0,则音量不变|如果值>1.0,则音量设置为1.0

get_volume()
get the playback volume
get_volume() -> value

返回一个从0.0到1.0的值,表示该声音的音量。

get_num_channels()
count how many times this Sound is playing
get_num_channels() -> count

返回该声音正在播放的活动声道数。

get_length()
get the length of the Sound
get_length() -> seconds

以秒为单位返回该声音的长度。

get_raw()
return a bytestring copy of the Sound samples.
get_raw() -> bytes

以字节形式返回Sound对象缓冲区的副本。

New in pygame 1.9.2.

pygame.mixer.Channel
Create a Channel object for controlling playback
Channel(id) -> Channel
— play a Sound on a specific Channel
— stop playback on a Channel
— temporarily stop playback of a channel
— resume pause playback of a channel
— stop playback after fading channel out
— set the volume of a playing channel
— get the volume of the playing channel
— check if the channel is active
— get the currently playing Sound
— queue a Sound object to follow the current
— return any Sound that is queued
— have the channel send an event when playback stops
— get the event a channel sends when playback stops

返回当前频道之一的Channel对象。ID必须是从0到 pygame.mixer.get_num_channels() 。

Channel对象可用于对声音的播放进行精细控制。一个频道一次只能播放一个声音。使用频道是完全可选的,因为默认情况下,PYGAME可以管理它们。

play()
play a Sound on a specific Channel
play(Sound, loops=0, maxtime=0, fade_ms=0) -> None

这将开始在特定频道上播放声音。如果该频道当前正在播放任何其他声音,它将被停止。

LOOPS参数的含义与中相同 Sound.play() :是指第一次之后重复声音的次数。如果为3,则声音将播放4次(第一次,然后再播放3次)。如果循环数为-1,则播放将无限期重复。

如中所示 Sound.play() ,MaxTime参数可用于在给定的毫秒数后停止播放声音。

如中所示 Sound.play() ,参数fade_ms可用于声音中的淡入淡出。

stop()
stop playback on a Channel
stop() -> None

停止频道上的声音播放。停止播放后,频道可以播放新的声音。

pause()
temporarily stop playback of a channel
pause() -> None

暂时停止频道上的声音播放。可以在以后通过以下方式恢复 Channel.unpause()

unpause()
resume pause playback of a channel
unpause() -> None

在暂停的频道上恢复播放。

fadeout()
stop playback after fading channel out
fadeout(time) -> None

在给定时间参数内淡出声音后停止播放频道(以毫秒为单位)。

set_volume()
set the volume of a playing channel
set_volume(value) -> None
set_volume(left, right) -> None

设置播放声音的音量(响度)。当频道开始播放时,其音量值将被重置。这只会影响当前声音。值参数介于0.0和1.0之间。

如果通过一个参数,它将是两个发言者的音量。如果传递了两个参数,并且混音器处于立体声模式,则第一个参数将是左侧扬声器的音量,第二个参数将是右侧扬声器的音量。(如果第二个参数是 None ,第一个参数将是两个发言者的音量。)

如果该频道正在播放的声音 set_volume() 也被调用,这两个调用都被考虑在内。例如:

sound = pygame.mixer.Sound("s.wav")
channel = s.play()      # Sound plays at full volume by default
sound.set_volume(0.9)   # Now plays at 90% of full volume.
sound.set_volume(0.6)   # Now plays at 60% (previous value replaced).
channel.set_volume(0.5) # Now plays at 30% (0.6 * 0.5).
get_volume()
get the volume of the playing channel
get_volume() -> value

返回当前播放声音的频道音量。这不考虑由 Channel.set_volume() 。Sound对象也有自己的音量,该音量与通道混合。

get_busy()
check if the channel is active
get_busy() -> bool

退货 True 如果通道正在活跃地混音。如果信道空闲,则返回 False 。

get_sound()
get the currently playing Sound
get_sound() -> Sound

返回当前在该频道上播放的实际Sound对象。如果该信道空闲 None 返回。

queue()
queue a Sound object to follow the current
queue(Sound) -> None

当声音在通道上排队时,它将在当前声音结束后立即开始播放。每个通道一次只能排队一个声音。只有当当前播放自动完成时,排队的声音才会播放。它在任何其他调用时被清除 Channel.stop() 或 Channel.play() 。

如果频道上没有正在播放的声音,则该声音将立即开始播放。

get_queue()
return any Sound that is queued
get_queue() -> Sound

如果声音已在此通道上排队,则会返回该声音。一旦排队的声音开始播放,它将不再在队列中。

set_endevent()
have the channel send an event when playback stops
set_endevent() -> None
set_endevent(type) -> None

当为通道设置了enventent时,每当该通道上的声音播放结束时(不只是第一次),它都会向pyGame队列发送一个事件。使用 pygame.event.get() 一旦被送出就能取回它。

请注意,如果你打电话给 Sound.play(n) 或 Channel.play(sound,n) ,End事件只发送一次:在声音播放了“n+1”次之后(请参阅Sound.play的文档)。

如果 Channel.stop() 或 Channel.play() 在声音仍在播放时被调用,则将立即发布该事件。

类型参数将是发送到队列的事件ID。这可以是任何有效的事件类型,但最好是以下值 pygame.locals.USEREVENT 和 pygame.locals.NUMEVENTS 。如果未给出类型参数,则通道将停止发送设备。

get_endevent()
get the event a channel sends when playback stops
get_endevent() -> type

返回每次通道完成声音播放时发送的事件类型。如果没有参与者,则函数返回 pygame.NOEVENT 。




Edit on GitHub