example: add audio_music_stream

This commit is contained in:
Frost-Phoenix 2025-02-04 19:38:52 +01:00 committed by Nikolas
parent 5004bb2316
commit 9882dd4ba1
3 changed files with 83 additions and 0 deletions

View File

@ -202,6 +202,11 @@ pub fn build(b: *std.Build) !void {
.path = "examples/audio/raw_stream.zig", .path = "examples/audio/raw_stream.zig",
.desc = "Plays a sine wave", .desc = "Plays a sine wave",
}, },
.{
.name = "music_stream",
.path = "examples/audio/music_stream.zig",
.desc = "Use music stream to play an audio file",
},
.{ .{
.name = "basic_screen_manager", .name = "basic_screen_manager",
.path = "examples/core/basic_screen_manager.zig", .path = "examples/core/basic_screen_manager.zig",

View File

@ -0,0 +1,78 @@
const rl = @import("raylib");
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
pub fn main() !void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
rl.initWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
defer rl.closeWindow(); // Close window and OpenGL context
rl.initAudioDevice(); // Initialize audio device
defer rl.closeAudioDevice(); // Close audio device (music streaming is automatically stopped)
const music: rl.Music = try rl.loadMusicStream("resources/audio/country.mp3");
defer rl.unloadMusicStream(music); // Unload music stream buffers from RAM
rl.playMusicStream(music);
var timePlayed: f32 = 0; // Time played normalized [0.0f..1.0f]
var pause: bool = false; // Music playing paused
rl.setTargetFPS(30); // Set our game to run at 30 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!rl.windowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
rl.updateMusicStream(music); // Update music buffer with new stream data
// Restart music playing (stop and play)
if (rl.isKeyPressed(rl.KeyboardKey.space)) {
rl.stopMusicStream(music);
rl.playMusicStream(music);
}
// Pause/Resume music playing
if (rl.isKeyPressed(rl.KeyboardKey.p)) {
pause = !pause;
if (pause) {
rl.pauseMusicStream(music);
} else {
rl.resumeMusicStream(music);
}
}
// Get normalized time played for current music stream
timePlayed = rl.getMusicTimePlayed(music) / rl.getMusicTimeLength(music);
if (timePlayed > 1.0) {
timePlayed = 1.0; // Make sure time played is no longer than music
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(rl.Color.white);
rl.drawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, rl.Color.light_gray);
rl.drawRectangle(200, 200, 400, 12, rl.Color.light_gray);
rl.drawRectangle(200, 200, @intFromFloat(timePlayed * 400), 12, rl.Color.maroon);
rl.drawRectangleLines(200, 200, 400, 12, rl.Color.gray);
rl.drawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, rl.Color.light_gray);
rl.drawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, rl.Color.light_gray);
//----------------------------------------------------------------------------------
}
}

BIN
resources/audio/country.mp3 Normal file

Binary file not shown.