0
# Audio and Video
1
2
Audio/video playback, 3D audio positioning, and media control.
3
4
## Quick Reference
5
6
```python
7
# Load
8
sound = pyglet.media.load('file.wav', streaming=False) # Small files
9
music = pyglet.media.load('file.mp3', streaming=True) # Large files
10
11
# Play
12
player = pyglet.media.Player()
13
player.queue(sound)
14
player.play()
15
16
# Control
17
player.pause()
18
player.seek(10.0) # Seek to 10 seconds
19
player.volume = 0.5 # 0.0-1.0
20
player.loop = True
21
```
22
23
## Media Loading
24
25
```python
26
def pyglet.media.load(filename: str, file=None, streaming=True) -> Source:
27
"""
28
Args:
29
filename: File path
30
file: File-like object (optional)
31
streaming: True = stream from disk, False = load into memory
32
33
Returns:
34
Source: Media source (audio or video)
35
"""
36
```
37
38
## Player Control
39
40
```python
41
class pyglet.media.Player:
42
"""Media player"""
43
__init__()
44
45
# Properties
46
playing: bool # Read-only
47
time: float # Current playback position (seconds)
48
volume: float # 0.0-1.0
49
min_distance, max_distance: float # 3D audio
50
position: tuple # (x, y, z) for 3D audio
51
pitch: float # Playback speed (1.0 = normal)
52
cone_orientation, cone_inner_angle, cone_outer_angle: float # 3D audio cone
53
source: Source # Current source (read-only)
54
55
# Methods
56
def queue(source: Source) # Add to queue
57
def play()
58
def pause()
59
def stop() # Stop and clear queue
60
def next_source() # Skip to next in queue
61
def seek(time: float) # Seek to position
62
63
# Events
64
def on_player_eos() # End of stream
65
def on_player_next_source() # Moving to next source
66
```
67
68
## Media Sources
69
70
```python
71
class pyglet.media.Source:
72
"""Abstract media source"""
73
duration: float # Length in seconds
74
video_format: VideoFormat | None
75
audio_format: AudioFormat | None
76
77
def play() -> Player # Convenience: create player and play
78
def get_queue_source() -> Source # For queueing
79
80
# Static source (fully loaded)
81
class pyglet.media.StaticSource(Source):
82
"""Source loaded into memory"""
83
84
# Streaming source
85
class pyglet.media.StreamingSource(Source):
86
"""Source streamed from disk"""
87
```
88
89
## Audio Synthesis
90
91
```python
92
# Generate tones
93
source = pyglet.media.synthesis.Sine(duration, frequency=440, sample_rate=44100)
94
source = pyglet.media.synthesis.Square(duration, frequency=440, sample_rate=44100)
95
source = pyglet.media.synthesis.Sawtooth(duration, frequency=440, sample_rate=44100)
96
source = pyglet.media.synthesis.Triangle(duration, frequency=440, sample_rate=44100)
97
source = pyglet.media.synthesis.WhiteNoise(duration, sample_rate=44100)
98
99
# Silence
100
source = pyglet.media.synthesis.Silence(duration, sample_rate=44100)
101
102
# Play
103
source.play()
104
```
105
106
## 3D Audio
107
108
```python
109
# Set listener position (camera)
110
listener = pyglet.media.get_audio_driver().get_listener()
111
listener.position = (0, 0, 0)
112
listener.forward_orientation = (0, 0, -1)
113
listener.up_orientation = (0, 1, 0)
114
115
# Set source position
116
player.position = (10, 0, 0) # 10 units to the right
117
player.min_distance = 1.0 # Full volume within this distance
118
player.max_distance = 100.0 # Silent beyond this distance
119
```
120
121
## Examples
122
123
### Background Music
124
```python
125
music = pyglet.media.load('background.mp3', streaming=True)
126
player = pyglet.media.Player()
127
player.queue(music)
128
player.loop = True
129
player.play()
130
```
131
132
### Sound Effects
133
```python
134
jump_sound = pyglet.media.load('jump.wav', streaming=False)
135
136
@window.event
137
def on_key_press(symbol, modifiers):
138
if symbol == pyglet.window.key.SPACE:
139
jump_sound.play() # Convenience method
140
```
141
142
### Music Playlist
143
```python
144
player = pyglet.media.Player()
145
songs = [
146
pyglet.media.load('song1.mp3', streaming=True),
147
pyglet.media.load('song2.mp3', streaming=True),
148
pyglet.media.load('song3.mp3', streaming=True),
149
]
150
for song in songs:
151
player.queue(song)
152
player.play()
153
154
@player.event
155
def on_player_eos():
156
print("Playlist finished")
157
```
158
159
### Volume Control
160
```python
161
master_volume = 0.7
162
music_player.volume = master_volume * 0.5 # Background music quieter
163
sfx_player.volume = master_volume * 1.0 # SFX at full
164
```
165
166
### Pause/Resume
167
```python
168
paused = False
169
170
@window.event
171
def on_key_press(symbol, modifiers):
172
global paused
173
if symbol == pyglet.window.key.P:
174
if paused:
175
player.play()
176
else:
177
player.pause()
178
paused = not paused
179
```
180
181
### Video Playback
182
```python
183
video = pyglet.media.load('movie.mp4')
184
player = pyglet.media.Player()
185
player.queue(video)
186
player.play()
187
188
# Get video texture
189
@window.event
190
def on_draw():
191
window.clear()
192
if player.source and player.source.video_format:
193
texture = player.get_texture()
194
if texture:
195
texture.blit(0, 0, width=window.width, height=window.height)
196
```
197
198
## Audio Drivers
199
200
```python
201
# Get available drivers
202
pyglet.media.get_audio_driver(name=None) # None = default
203
204
# Set driver order (in pyglet.options before importing)
205
pyglet.options['audio'] = ('openal', 'pulse', 'xaudio2', 'directsound', 'silent')
206
```
207
208
## Performance Tips
209
210
1. **Streaming for large files**: Use `streaming=True` for music
211
2. **Static for small files**: Use `streaming=False` for SFX
212
3. **Preload SFX**: Load sound effects during init
213
4. **Limit concurrent players**: Too many causes audio issues
214
5. **Queue sources**: More efficient than recreating players
215
216
## Common Issues
217
218
1. **No audio**: Check `pyglet.options['audio']` driver order
219
2. **Choppy streaming**: File I/O bottleneck, use SSD or reduce count
220
3. **Video sync**: Video playback is experimental, may have issues
221
4. **Seek limitations**: Some formats don't support seeking
222
5. **Format support**: Depends on available codecs (FFmpeg recommended)
223