raylib/ports/audio/audio_music_stream
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
audio_music_stream.odin ¶100 linesSource
1/*******************************************************************************************
2*
3* raylib [audio] example - music stream
4*
5* Example complexity rating: [★☆☆☆] 1/4
6*
7* Example originally created with raylib 1.3, last time updated with raylib 4.2
8*
9* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10* BSD-like license that allows static linking with closed source software
11*
12* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
13*
14********************************************************************************************/
15
16package raylib_examples
17
18import rl "vendor:raylib"
19
20//------------------------------------------------------------------------------------
21// Program main entry point
22//------------------------------------------------------------------------------------
23main :: proc() {
24 // Initialization
25 //--------------------------------------------------------------------------------------
26 SCREEN_WIDTH :: 800
27 SCREEN_HEIGHT :: 450
28
29 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [audio] example - music stream")
30
31 rl.InitAudioDevice() // Initialize audio device
32
33 music := rl.LoadMusicStream("resources/country.mp3")
34
35 rl.PlayMusicStream(music)
36
37 time_played: f32 // Time played normalized [0.0f..1.0f]
38 pause: bool // Music playing paused
39
40 rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
41 //--------------------------------------------------------------------------------------
42
43 // Main game loop
44 for !rl.WindowShouldClose() { // Detect window close button or ESC key
45 // Update
46 //----------------------------------------------------------------------------------
47 rl.UpdateMusicStream(music) // Update music buffer with new stream data
48
49 // Restart music playing (stop and play)
50 if rl.IsKeyPressed(.SPACE) {
51 rl.StopMusicStream(music)
52 rl.PlayMusicStream(music)
53 }
54
55 // Pause/Resume music playing
56 if rl.IsKeyPressed(.P) {
57 pause = !pause
58
59 if pause {
60 rl.PauseMusicStream(music)
61 } else {
62 rl.ResumeMusicStream(music)
63 }
64 }
65
66 // Get normalized time played for current music stream
67 time_played = rl.GetMusicTimePlayed(music)/rl.GetMusicTimeLength(music)
68
69 if time_played > 1 {
70 time_played = 1 // Make sure time played is no longer than music
71 }
72 //----------------------------------------------------------------------------------
73
74 // Draw
75 //----------------------------------------------------------------------------------
76 rl.BeginDrawing()
77
78 rl.ClearBackground(rl.RAYWHITE)
79
80 rl.DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, rl.LIGHTGRAY)
81
82 rl.DrawRectangle(200, 200, 400, 12, rl.LIGHTGRAY)
83 rl.DrawRectangle(200, 200, i32(time_played*400), 12, rl.MAROON)
84 rl.DrawRectangleLines(200, 200, 400, 12, rl.GRAY)
85
86 rl.DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, rl.LIGHTGRAY)
87 rl.DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, rl.LIGHTGRAY)
88
89 rl.EndDrawing()
90 //----------------------------------------------------------------------------------
91 }
92
93 // De-Initialization
94 //--------------------------------------------------------------------------------------
95 rl.UnloadMusicStream(music) // Unload music stream buffers from RAM
96
97 rl.CloseAudioDevice() // Close audio device (music streaming is automatically stopped)
98 rl.CloseWindow() // Close window and OpenGL context
99 //--------------------------------------------------------------------------------------
100}
Assets 1
Declarations Used 26
- vendor:raylib BeginDrawing · ClearBackground · CloseAudioDevice · CloseWindow · DrawRectangle · DrawRectangleLines · DrawText · EndDrawing · GetMusicTimeLength · GetMusicTimePlayed · GRAY · InitAudioDevice · InitWindow · IsKeyPressed · LIGHTGRAY · LoadMusicStream · MAROON · PauseMusicStream · PlayMusicStream · RAYWHITE · ResumeMusicStream · SetTargetFPS · StopMusicStream · UnloadMusicStream · UpdateMusicStream · WindowShouldClose