Class cocos.audio.pygame.mixer.Channel

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.

Methods

  __init__(self, id)
Create a Channel object for controlling playback.
  __new__(cls, id)
  fadeout(self, time)
Stop playback after fading channel out.
bool get_busy(self)
Determine if the channel is active.
int get_endevent(self)
Get the event a channel sends when playback stops.
Sound get_queue(self)
Return any Sound that is queued.
Sound get_sound(self)
Get the currently playing Sound.
float get_volume(self)
Get the volume of the playing channel.
  pause(self)
Temporarily stop playback of a channel.
  play(self, sound, loops=0, time=-1)
Play a Sound on a specific Channel.
  queue(self, sound)
Queue a Sound object to follow the current.
  set_endevent(self, id=None)
Have the channel send an event when playback stops.
  set_volume(self, left, right=None)
Set the volume of a playing channel.
  stop(self)
Stop playback on a Channel.
  unpause(self)
Resume pause playback of a channel.

Method Details

__init__

(Constructor) __init__(self, id)

Create a Channel object for controlling playback.

Create 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().

Parameters:
id : int
ID of existing channel to create object for.

fadeout

fadeout(self, time)

Stop playback after fading channel out.

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

Parameters:
time : int
Time to fade out, in milliseconds.

get_busy

get_busy(self)

Determine if the channel is active.

Returns true if the channel is activily mixing sound. If the channel is idle this returns False.

Returns: bool

get_endevent

get_endevent(self)

Get the event a channel sends when playback stops.

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.

Returns: int

get_queue

get_queue(self)

Return any Sound that is queued.

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.

Returns: Sound

get_sound

get_sound(self)

Get the currently playing Sound.

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

Returns: Sound

get_volume

get_volume(self)

Get the volume of the playing channel.

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.

Returns: float

pause

pause(self)

Temporarily stop playback of a channel.

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

play

play(self, sound, loops=0, time=-1)

Play a Sound on a specific Channel.

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 time argument can be used to stop playback of the Sound after a given number of milliseconds.

Parameters:
sound : Sound
Sound data to play.
loops : int
Number of times to repeat the sound after the first play.
time : int
Maximum number of milliseconds to play for.

queue

queue(self, sound)

Queue a Sound object to follow the current.

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.

Parameters:
sound : Sound
Sound data to queue.

set_endevent

set_endevent(self, id=None)

Have the channel send an event when playback stops.

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 id 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.

Parameters:
id : int
Event ID to send.

set_volume

set_volume(self, left, right=None)

Set the volume of a playing channel.

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. Each 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).
Parameters:
left : float
Volume of left (or mono) channel, in range [0.0, 1.0]
right : float
Volume of right channel, in range [0.0, 1.0]

stop

stop(self)

Stop playback on a Channel.

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

unpause

unpause(self)

Resume pause playback of a channel.

Resume the playback on a paused channel.