diff options
Diffstat (limited to 'examples/audio_music_stream.c')
| -rw-r--r-- | examples/audio_music_stream.c | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/examples/audio_music_stream.c b/examples/audio_music_stream.c index e135a6e4..dc9d4355 100644 --- a/examples/audio_music_stream.c +++ b/examples/audio_music_stream.c @@ -24,11 +24,12 @@ int main() InitAudioDevice(); // Initialize audio device - PlayMusicStream(0, "resources/audio/guitar_noodling.ogg"); // Play music stream + Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg"); + + PlayMusicStream(music); - int framesCounter = 0; float timePlayed = 0.0f; - //float volume = 1.0; + bool pause = false; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -38,32 +39,26 @@ int main() { // Update //---------------------------------------------------------------------------------- - framesCounter++; - - // Testing music fading from one file to another -/* - if (framesCounter > 600) // Wait for 10 seconds (600 frames) + UpdateMusicStream(music); // Update music buffer with new stream data + + // Restart music playing (stop and play) + if (IsKeyPressed(KEY_SPACE)) { - volume -= 0.01; // Decrement music volume level - - // When music volume level equal or lower than 0, - // restore volume level and init another music file - if (volume <= 0) - { - volume = 1.0; - framesCounter = 0; - PlayMusicStream(1, "resources/audio/another_file.ogg"); - } - - SetMusicVolume(volume); + StopMusicStream(music); + PlayMusicStream(music); } -*/ - if (IsWindowMinimized()) PauseMusicStream(0); - else ResumeMusicStream(0); - - timePlayed = GetMusicTimePlayed(0)/GetMusicTimeLength(0)*100*4; // We scale by 4 to fit 400 pixels - UpdateMusicStream(0); // Update music buffer with new stream data + // Pause/Resume music playing + if (IsKeyPressed(KEY_P)) + { + pause = !pause; + + if (pause) PauseMusicStream(music); + else ResumeMusicStream(music); + } + + // Get timePlayed scaled to bar dimensions (400 pixels) + timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*100*4; //---------------------------------------------------------------------------------- // Draw @@ -72,10 +67,14 @@ int main() ClearBackground(RAYWHITE); - DrawText("MUSIC SHOULD BE PLAYING!", 255, 200, 20, LIGHTGRAY); + DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY); - DrawRectangle(200, 250, 400, 12, LIGHTGRAY); - DrawRectangle(200, 250, (int)timePlayed, 12, MAROON); + DrawRectangle(200, 200, 400, 12, LIGHTGRAY); + DrawRectangle(200, 200, (int)timePlayed, 12, MAROON); + DrawRectangleLines(200, 200, 400, 12, GRAY); + + DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY); + DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY); EndDrawing(); //---------------------------------------------------------------------------------- @@ -83,9 +82,11 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) + UnloadMusicStream(music); // Unload music stream buffers from RAM + + CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; |
