Package pyglet.media

Audio and video playback.

pyglet can play WAV files, and if AVbin is installed, many other audio and video formats.

Playback is handled by the Player class, which reads raw data from Source objects and provides methods for pausing, seeking, adjusting the volume, and so on. The Player class implements a the best available audio device (currently, only OpenAL is supported):

player = Player()

A Source is used to decode arbitrary audio and video files. It is associated with a single player by "queuing" it:

source = load('background_music.mp3')
player.queue(source)

Use the Player to control playback.

If the source contains video, the Source.video_format attribute will be non-None, and the Player.texture attribute will contain the current video image synchronised to the audio.

Decoding sounds can be processor-intensive and may introduce latency, particularly for short sounds that must be played quickly, such as bullets or explosions. You can force such sounds to be decoded and retained in memory rather than streamed from disk by wrapping the source in a StaticSource:

bullet_sound = StaticSource(load('bullet.wav'))

The other advantage of a StaticSource is that it can be queued on any number of players, and so played many times simultaneously.

Classes

  MediaException
  MediaFormatException
  CannotSeekException
  AudioFormat
Audio details.
  VideoFormat
Video details.
  AudioData
A single packet of audio data.
  AudioPlayer
Abstract low-level interface for playing audio.
  Source
An audio and/or video source.
  StreamingSource
A source that is decoded as it is being played, and can only be queued once.
  StaticSource
A source that has been completely decoded in memory.
  StaticMemorySource
Helper class for default implementation of StaticSource.
  Player
A sound and/or video player.
  ManagedSoundPlayer
A player which takes care of updating its own audio buffers.
  Listener
The listener properties for positional audio.

Functions

Source load(filename, file=None, streaming=True)
Load a source from a file.
  dispatch_events()
Process managed audio events.

Variables

  driver = None
  audio_player_class = driver.driver_audio_player_class
Listener listener = driver.driver_listener
The singleton listener.
bool have_avbin = False
Indication of the presence of AVbin.
  managed_players = []

Function Details

load

load(filename, file=None, streaming=True)

Load a source from a file.

Currently the file argument is not supported; media files must exist as real paths.

Parameters:
filename : str
Filename of the media file to load.
file : file-like object
Not yet supported.
streaming : bool
If False, a StaticSource will be returned; otherwise (default) a StreamingSource is created.
Returns: Source

dispatch_events

dispatch_events()

Process managed audio events.

You must call this function regularly (typically once per run loop iteration) in order to keep audio buffers of managed players full.

Deprecated: Since pyglet 1.1, Player objects schedule themselves on the default clock automatically. Applications should not call this method.


Variables Details

have_avbin

Indication of the presence of AVbin. When have_avbin is True pyglet will be able to play back compressed media streams such as MP3, OGG and various video formats. If False only uncompressed Wave files can be loaded.
Type:
bool
Value:
False