Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback
Audio/video playback, 3D audio positioning, and media control.
# Load
sound = pyglet.media.load('file.wav', streaming=False) # Small files
music = pyglet.media.load('file.mp3', streaming=True) # Large files
# Play
player = pyglet.media.Player()
player.queue(sound)
player.play()
# Control
player.pause()
player.seek(10.0) # Seek to 10 seconds
player.volume = 0.5 # 0.0-1.0
player.loop = Truedef pyglet.media.load(filename: str, file=None, streaming=True) -> Source:
"""
Args:
filename: File path
file: File-like object (optional)
streaming: True = stream from disk, False = load into memory
Returns:
Source: Media source (audio or video)
"""class pyglet.media.Player:
"""Media player"""
__init__()
# Properties
playing: bool # Read-only
time: float # Current playback position (seconds)
volume: float # 0.0-1.0
min_distance, max_distance: float # 3D audio
position: tuple # (x, y, z) for 3D audio
pitch: float # Playback speed (1.0 = normal)
cone_orientation, cone_inner_angle, cone_outer_angle: float # 3D audio cone
source: Source # Current source (read-only)
# Methods
def queue(source: Source) # Add to queue
def play()
def pause()
def stop() # Stop and clear queue
def next_source() # Skip to next in queue
def seek(time: float) # Seek to position
# Events
def on_player_eos() # End of stream
def on_player_next_source() # Moving to next sourceclass pyglet.media.Source:
"""Abstract media source"""
duration: float # Length in seconds
video_format: VideoFormat | None
audio_format: AudioFormat | None
def play() -> Player # Convenience: create player and play
def get_queue_source() -> Source # For queueing
# Static source (fully loaded)
class pyglet.media.StaticSource(Source):
"""Source loaded into memory"""
# Streaming source
class pyglet.media.StreamingSource(Source):
"""Source streamed from disk"""# Generate tones
source = pyglet.media.synthesis.Sine(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Square(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Sawtooth(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.Triangle(duration, frequency=440, sample_rate=44100)
source = pyglet.media.synthesis.WhiteNoise(duration, sample_rate=44100)
# Silence
source = pyglet.media.synthesis.Silence(duration, sample_rate=44100)
# Play
source.play()# Set listener position (camera)
listener = pyglet.media.get_audio_driver().get_listener()
listener.position = (0, 0, 0)
listener.forward_orientation = (0, 0, -1)
listener.up_orientation = (0, 1, 0)
# Set source position
player.position = (10, 0, 0) # 10 units to the right
player.min_distance = 1.0 # Full volume within this distance
player.max_distance = 100.0 # Silent beyond this distancemusic = pyglet.media.load('background.mp3', streaming=True)
player = pyglet.media.Player()
player.queue(music)
player.loop = True
player.play()jump_sound = pyglet.media.load('jump.wav', streaming=False)
@window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.SPACE:
jump_sound.play() # Convenience methodplayer = pyglet.media.Player()
songs = [
pyglet.media.load('song1.mp3', streaming=True),
pyglet.media.load('song2.mp3', streaming=True),
pyglet.media.load('song3.mp3', streaming=True),
]
for song in songs:
player.queue(song)
player.play()
@player.event
def on_player_eos():
print("Playlist finished")master_volume = 0.7
music_player.volume = master_volume * 0.5 # Background music quieter
sfx_player.volume = master_volume * 1.0 # SFX at fullpaused = False
@window.event
def on_key_press(symbol, modifiers):
global paused
if symbol == pyglet.window.key.P:
if paused:
player.play()
else:
player.pause()
paused = not pausedvideo = pyglet.media.load('movie.mp4')
player = pyglet.media.Player()
player.queue(video)
player.play()
# Get video texture
@window.event
def on_draw():
window.clear()
if player.source and player.source.video_format:
texture = player.get_texture()
if texture:
texture.blit(0, 0, width=window.width, height=window.height)# Get available drivers
pyglet.media.get_audio_driver(name=None) # None = default
# Set driver order (in pyglet.options before importing)
pyglet.options['audio'] = ('openal', 'pulse', 'xaudio2', 'directsound', 'silent')streaming=True for musicstreaming=False for SFXpyglet.options['audio'] driver orderInstall with Tessl CLI
npx tessl i tessl/pypi-pyglet