Audio and video playback is handled by the same package, pyglet.media, which automatically detects the media type and acts appropriately. As I mentioned in Pyglet: Helpful Tips, you will want to install the "optional" AVbin to support anything other than plain uncompressed formats.
Though you might want to refer to the API docs for all the details, I'll explain here how to set up sound playback. In brief, you use a Source instance to decode an audio file and then queue this on a Player for playback. By default this decoding is done on-the-fly as a StreamingSource. If you want a sound to be decoded in advance, you can designate it as a StaticSource. This is useful for short sound effects and the like. There's not much you can do with a source except read its duration.
A Player can be used to play, pause and restart a sound. You can get the current playback point and seek to a position in the source. There is support for pitch shifting and attenuation, but I did not try these. There is no time stretching, however, and even basic filtering or EQ is outside the realm of what this simple library is designed to do. I would like to see these features, as well as the ability to cross-fade sources, as this would provide commonly needed functionality.
Here is the basic code I used:
import pyglet
wavfile = 'some/path/to/file.wav'
sound = pyglet.media.load(wavfile)
core = pyglet.media.Player()
core.queue(sound)
core.play()
There is only one event provided for responding to audio, on_eos(), which triggers when the player has reached the end of the current source. It would be great to be able to set up triggers at other times (so one could have advanced notice of the approaching end of a file) and on other events (for example, pausing).
This library for sufficient for my simple case, but I would have to look at something more complete for more than the rudiments... a search that still continues!
A Player can be used to play, pause and restart a sound. You can get the current playback point and seek to a position in the source. There is support for pitch shifting and attenuation, but I did not try these. There is no time stretching, however, and even basic filtering or EQ is outside the realm of what this simple library is designed to do. I would like to see these features, as well as the ability to cross-fade sources, as this would provide commonly needed functionality.
Here is the basic code I used:
import pyglet
wavfile = 'some/path/to/file.wav'
sound = pyglet.media.load(wavfile)
core = pyglet.media.Player()
core.queue(sound)
core.play()
There is only one event provided for responding to audio, on_eos(), which triggers when the player has reached the end of the current source. It would be great to be able to set up triggers at other times (so one could have advanced notice of the approaching end of a file) and on other events (for example, pausing).
This library for sufficient for my simple case, but I would have to look at something more complete for more than the rudiments... a search that still continues!