用于加载和播放声音的 pygame 模块

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.mixer。 顶层 pygame.init() 会自动处理这个问题,但不能将任何参数传递给混音器 init。 为了解决这个问题,mixer 有一个函数 pygame.mixer.pre_init() 在使用顶层初始化之前设置正确的默认值。

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


多次调用它是安全的,但是在初始化混音器之后,如果不先调用 pygame.mixer.quit(),就无法更改播放参数。

在 pygame 1.8 中更改:默认缓冲区大小从 1024 更改为 3072。

在 pygame 1.9.1 中更改:默认缓冲区大小从 3072 更改为 4096。

在 pygame 2.0.0 中更改:默认缓冲区大小从 4096 更改为 512。

在 pygame 2.0.0 中更改:默认频率从 22050 更改为 44100。

在 pygame 2.0.0 中更改:大小可以是 32(32 位浮点数)。

在 pygame 2.0.0 中更改:通道也可以是 4 或 6。

pygame 2.0.0 中的新功能:添加了允许的更改、设备名称参数

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

Call pre_init to change the defaults used when the real pygame.mixer.init() is called. Keyword arguments are accepted. The best way to set custom mixer playback values is to call pygame.mixer.pre_init() before calling the top level pygame.init(). For backwards compatibility, argument values of 0 are replaced with the startup defaults, except for allowedchanges, where -1 is used.

Changed in pygame 1.8: The default buffersize changed from 1024 to 3072.

Changed in pygame 1.9.1: The default buffersize changed from 3072 to 4096.

Changed in pygame 2.0.0: The default buffersize changed from 4096 to 512.

Changed in pygame 2.0.0: The default frequency changed from 22050 to 44100.

New in pygame 2.0.0: allowedchanges, devicename arguments added

uninitialize the mixer quit() → None

This will uninitialize pygame.mixerpygame module for loading and playing sounds. All playback will stop and any loaded Sound objects may not be compatible with the mixer if it is reinitialized later.

test if the mixer is initialized get_init() → (frequency, format, channels)

If the mixer is initialized, this returns the playback arguments it is using. If the mixer has not been initialized this returns None. pygame.mixer.stop() stop playback of all sound channels stop() → None

This will stop all playback of all active mixer channels.

temporarily stop playback of all sound channels pause() → None

This will temporarily stop all playback on the active mixer channels. The playback can later be resumed with pygame.mixer.unpause()

resume paused playback of sound channels unpause() → None

This will resume all active sound channels after they have been paused.

fade out the volume on all sounds before stopping fadeout(time) → None

This will fade out the volume on all active channels over the time argument in milliseconds. After the sound is muted the playback will stop.

set the total number of playback channels set_num_channels(count) → None

Sets the number of available channels for the mixer. The default value is 8. The value can be increased or decreased. If the value is decreased, sounds playing on the truncated channels are stopped.

get the total number of playback channels get_num_channels() → count

Returns the number of currently active playback channels.

reserve channels from being automatically used set_reserved(count) → count

The mixer can reserve any number of channels that will not be automatically selected for playback by Sounds. If sounds are currently playing on the reserved channels they will not be stopped.

This allows the application to reserve a specific number of channels for important sounds that must not be dropped or have a guaranteed channel to play on.

Will return number of channels actually reserved, this may be less than requested depending on the number of channels previously allocated.

find an unused channel find_channel(force=False) → Channel

This will find and return an inactive Channel object. If there are no inactive Channels this function will return None. If there are no inactive channels and the force argument is True, this will find the Channel with the longest running Sound and return it.

test if any sound is being mixed get_busy() → bool

Returns True if the mixer is busy mixing any channels. If the mixer is idle then this return 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) – if True (default) the linked version number is returned, otherwise the compiled version number is returned Returns

the mixer's SDL library version number (linked or compiled depending on the linked parameter) as a tuple of 3 integers (major, minor, patch) Return type



The linked and compile version numbers should be the same.

New in pygame 2.0.0.

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

Load a new sound buffer from a filename, a python file object or a readable buffer object. Limited resampling will be performed to help the sample match the initialize arguments for the mixer. A Unicode string can only be a file pathname. A Python 2.x string or a Python 3.x bytes object can be either a pathname or a buffer object. Use the 'file' or 'buffer' keywords to avoid ambiguity; otherwise Sound may guess wrong. If the array keyword is used, the object is expected to export a version 3, C level array interface or, for Python 2.6 or later, a new buffer interface (The object is checked for a buffer interface first.)

The Sound object represents actual sound sample data. Methods that change the state of the Sound object will the all instances of the Sound playback. A Sound object also exports an array interface, and, for Python 2.6 or later, a new buffer interface.

The Sound can be loaded from an OGG audio file or from an uncompressed WAV.

Note: The buffer will be copied internally, no data will be shared between it and the Sound object.

For now buffer and array support is consistent with sndarray.make_sound for Numeric arrays, in that sample sign and byte order are ignored. This will change, either by correctly handling sign and byte order, or by raising an exception when different. Also, source samples are truncated to fit the audio sample size. This will not change.

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 keyword arguments and array interface support

New in pygame 2.0.1: pathlib.Path support on Python 3.

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

Begin playback of the Sound (i.e., on the computer's speakers) on an available Channel. This will forcibly select a Channel, so playback may cut off a currently playing sound if necessary.

The loops argument controls how many times the sample will be repeated after being played the first time. A value of 5 means that the sound will be played once, then repeated five times, and so is played a total of six times. The default value (zero) means the Sound is not repeated, and so is only played once. If loops is set to -1 the Sound will loop indefinitely (though you can still call stop() to stop it).

The maxtime argument can be used to stop playback after a given number of milliseconds.

The fade_ms argument will make the sound start playing at 0 volume and fade up to full volume over the time given. The sample may end before the fade-in is complete.

This returns the Channel object for the channel that was selected.

stop sound playback stop() → None

This will stop the playback of this Sound on any active Channels.

stop sound playback after fading out fadeout(time) → None

This will stop playback of the sound after fading it out over the time argument in milliseconds. The Sound will fade and stop on all actively playing channels. set_volume() set the playback volume for this Sound set_volume(value) → None

This will set the playback volume (loudness) for this Sound. This will immediately affect the Sound if it is playing. It will also affect any future playback of this Sound.


value (float) –

volume in the range of 0.0 to 1.0 (inclusive) If value < 0.0, the volume will not be changed If value > 1.0, the volume will be set to 1.0

get the playback volume get_volume() → value

Return a value from 0.0 to 1.0 representing the volume for this Sound.

count how many times this Sound is playing get_num_channels() → count

Return the number of active channels this sound is playing on.

get the length of the Sound get_length() → seconds

Return the length of this Sound in seconds.

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

Return a copy of the Sound object buffer as a bytes (for Python 3.x) or str (for Python 2.x) object.

New in pygame 1.9.2.

Create a Channel object for controlling playback Channel(id) → Channel

Return a Channel object for one of the current channels. The id must be a value from 0 to the value of pygame.mixer.get_num_channels().

The Channel object can be used to get fine control over the playback of Sounds. A channel can only playback a single Sound at time. Using channels is entirely optional since pygame can manage them by default.

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

This will begin playback of a Sound on a specific Channel. If the Channel is currently playing any other Sound it will be stopped.

The loops argument has the same meaning as in Sound.play(): it is the number of times to repeat the sound after the first time. If it is 3, the sound will be played 4 times (the first time, then three more). If loops is -1 then the playback will repeat indefinitely.

As in Sound.play(), the maxtime argument can be used to stop playback of the Sound after a given number of milliseconds.

As in Sound.play(), the fade_ms argument can be used fade in the sound.

stop playback on a Channel stop() → None

Stop sound playback on a channel. After playback is stopped the channel becomes available for new Sounds to play on it.

temporarily stop playback of a channel pause() → None

Temporarily stop the playback of sound on a channel. It can be resumed at a later time with Channel.unpause()

resume pause playback of a channel unpause() → None

Resume the playback on a paused channel.

stop playback after fading channel out fadeout(time) → None

Stop playback of a channel after fading out the sound over the given time argument in milliseconds.

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

Set the volume (loudness) of a playing sound. When a channel starts to play its volume value is reset. This only affects the current sound. The value argument is between 0.0 and 1.0.

If one argument is passed, it will be the volume of both speakers. If two arguments are passed and the mixer is in stereo mode, the first argument will be the volume of the left speaker and the second will be the volume of the right speaker. (If the second argument is None, the first argument will be the volume of both speakers.)

If the channel is playing a Sound on which set_volume() has also been called, both calls are taken into account. For example:

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 the volume of the playing channel get_volume() → value

Return the volume of the channel for the current playing sound. This does not take into account stereo separation used by Channel.set_volume(). The Sound object also has its own volume which is mixed with the channel.

check if the channel is active get_busy() → bool

Returns True if the channel is actively mixing sound. If the channel is idle this returns False.

get the currently playing Sound get_sound() → Sound

Return the actual Sound object currently playing on this channel. If the channel is idle None is returned.

queue a Sound object to follow the current queue(Sound) → None

When a Sound is queued on a Channel, it will begin playing immediately after the current Sound is finished. Each channel can only have a single Sound queued at a time. The queued Sound will only play if the current playback finished automatically. It is cleared on any other call to Channel.stop() or Channel.play().

If there is no sound actively playing on the Channel then the Sound will begin playing immediately.

return any Sound that is queued get_queue() → Sound

If a Sound is already queued on this channel it will be returned. Once the queued sound begins playback it will no longer be on the queue.

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

When an endevent is set for a channel, it will send an event to the pygame queue every time a sound finishes playing on that channel (not just the first time). Use pygame.event.get() to retrieve the endevent once it's sent.

Note that if you called Sound.play(n) or Channel.play(sound,n), the end event is sent only once: after the sound has been played “n+1” times (see the documentation of Sound.play).

If Channel.stop() or Channel.play() is called while the sound was still playing, the event will be posted immediately.

The type argument will be the event id sent to the queue. This can be any valid event type, but a good choice would be a value between pygame.locals.USEREVENT and pygame.locals.NUMEVENTS. If no type argument is given then the Channel will stop sending endevents.

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

Returns the event type to be sent every time the Channel finishes playback of a Sound. If there is no endevent the function returns pygame.NOEVENT.

  • zh/pygame/mixer.txt
  • 最后更改: 16月前
  • milowork