Files
raylib/examples/audio/audio_music_stream.c

101 lines
3.7 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [audio] example - music stream
*
* Example complexity rating: [] 1/4
*
* Example originally created with raylib 1.3, last time updated with raylib 4.2
*
2022-07-20 01:28:37 +02:00
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2022-06-21 19:53:18 +02:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
const int screenWidth = 800;
const int screenHeight = 450;
2014-09-29 23:41:05 +02:00
InitWindow(screenWidth, screenHeight, "raylib [audio] example - music stream");
2014-09-29 23:41:05 +02:00
InitAudioDevice(); // Initialize audio device
2014-09-29 23:41:05 +02:00
Music music = LoadMusicStream("resources/country.mp3");
2019-05-20 16:36:42 +02:00
PlayMusicStream(music);
2014-09-29 23:41:05 +02:00
2022-07-29 12:08:04 +02:00
float timePlayed = 0.0f; // Time played normalized [0.0f..1.0f]
bool pause = false; // Music playing paused
2022-11-08 22:47:05 +02:00
SetTargetFPS(30); // Set our game to run at 30 frames-per-second
//--------------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
UpdateMusicStream(music); // Update music buffer with new stream data
2025-08-07 17:08:22 +02:00
2022-08-02 10:23:54 +02:00
// Restart music playing (stop and play)
if (IsKeyPressed(KEY_SPACE))
{
StopMusicStream(music);
PlayMusicStream(music);
}
// Pause/Resume music playing
if (IsKeyPressed(KEY_P))
{
pause = !pause;
if (pause) PauseMusicStream(music);
else ResumeMusicStream(music);
}
2019-05-20 16:36:42 +02:00
2022-07-29 12:08:04 +02:00
// Get normalized time played for current music stream
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music);
2022-07-29 12:08:04 +02:00
if (timePlayed > 1.0f) timePlayed = 1.0f; // Make sure time played is no longer than music
//----------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2014-09-29 23:41:05 +02:00
ClearBackground(RAYWHITE);
2014-09-29 23:41:05 +02:00
DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
2014-09-29 23:41:05 +02:00
DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
2022-07-29 12:08:04 +02:00
DrawRectangle(200, 200, (int)(timePlayed*400.0f), 12, MAROON);
DrawRectangleLines(200, 200, 400, 12, GRAY);
2019-05-20 16:36:42 +02:00
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
2014-09-29 23:41:05 +02:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadMusicStream(music); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
2014-09-29 23:41:05 +02:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
return 0;
2022-11-08 22:47:05 +02:00
}