Skip to content

Commit c58d89f

Browse files
add example
1 parent f551fca commit c58d89f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

examples/audio/audio_music_stream.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""checked with raylib-python-cffi 5.5.0.2
2+
raylib [audio] example - Music playing (streaming)
3+
Example complexity rating: [★☆☆☆] 1/4
4+
Example originally created with raylib 1.3, last time updated with raylib 4.0
5+
Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
6+
BSD-like license that allows static linking with closed source software
7+
Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
8+
9+
This source has been converted from C raylib examples to Python.
10+
"""
11+
12+
import pyray as rl
13+
from pathlib import Path
14+
15+
THIS_DIR = Path(__file__).resolve().parent
16+
17+
18+
# ------------------------------------------------------------------------------------
19+
# Program main entry point
20+
# ------------------------------------------------------------------------------------
21+
def main():
22+
# Initialization
23+
# --------------------------------------------------------------------------------------
24+
screen_width = 800
25+
screen_height = 450
26+
27+
rl.init_window(
28+
screen_width,
29+
screen_height,
30+
"raylib [audio] example - music playing (streaming)",
31+
)
32+
33+
rl.init_audio_device() # Initialize audio device
34+
35+
music = rl.load_music_stream(str(THIS_DIR / "resources/country.mp3"))
36+
37+
rl.play_music_stream(music)
38+
39+
time_played = 0.0 # Time played normalized [0.0f..1.0f]
40+
pause = False # Music playing paused
41+
42+
rl.set_target_fps(30) # Set our game to run at 30 frames-per-second
43+
# --------------------------------------------------------------------------------------
44+
45+
# Main game loop
46+
while not rl.window_should_close(): # Detect window close button or ESC key
47+
# Update
48+
# ----------------------------------------------------------------------------------
49+
rl.update_music_stream(music) # Update music buffer with new stream data
50+
51+
# Restart music playing (stop and play)
52+
if rl.is_key_pressed(rl.KEY_SPACE):
53+
rl.stop_music_stream(music)
54+
rl.play_music_stream(music)
55+
56+
# Pause/Resume music playing
57+
if rl.is_key_pressed(rl.KEY_P):
58+
pause = not pause
59+
60+
if pause:
61+
rl.pause_music_stream(music)
62+
else:
63+
rl.resume_music_stream(music)
64+
65+
# Get normalized time played for current music stream
66+
time_played = rl.get_music_time_played(music) / rl.get_music_time_length(music)
67+
68+
if time_played > 1.0:
69+
time_played = 1.0 # Make sure time played is no longer than music
70+
# ----------------------------------------------------------------------------------
71+
72+
# Draw
73+
# ----------------------------------------------------------------------------------
74+
rl.begin_drawing()
75+
76+
rl.clear_background(rl.RAYWHITE)
77+
78+
rl.draw_text("MUSIC SHOULD BE PLAYING!", 255, 150, 20, rl.LIGHTGRAY)
79+
80+
rl.draw_rectangle(200, 200, 400, 12, rl.LIGHTGRAY)
81+
rl.draw_rectangle(200, 200, int(time_played * 400.0), 12, rl.MAROON)
82+
rl.draw_rectangle_lines(200, 200, 400, 12, rl.GRAY)
83+
84+
rl.draw_text("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, rl.LIGHTGRAY)
85+
rl.draw_text("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, rl.LIGHTGRAY)
86+
87+
rl.end_drawing()
88+
# ----------------------------------------------------------------------------------
89+
90+
# De-Initialization
91+
# --------------------------------------------------------------------------------------
92+
rl.unload_music_stream(music) # Unload music stream buffers from RAM
93+
94+
rl.close_audio_device() # Close audio device (music streaming is automatically stopped)
95+
96+
rl.close_window() # Close window and OpenGL context
97+
# --------------------------------------------------------------------------------------
98+
99+
100+
if __name__ == "__main__":
101+
main()

0 commit comments

Comments
 (0)