audio_music_stream.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Music playing (streaming)
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  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. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
  26. InitAudioDevice(); // Initialize audio device
  27. Music music = LoadMusicStream("resources/country.mp3");
  28. PlayMusicStream(music);
  29. float timePlayed = 0.0f; // Time played normalized [0.0f..1.0f]
  30. bool pause = false; // Music playing paused
  31. SetTargetFPS(30); // Set our game to run at 30 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. UpdateMusicStream(music); // Update music buffer with new stream data
  39. // Restart music playing (stop and play)
  40. if (IsKeyPressed(KEY_SPACE))
  41. {
  42. StopMusicStream(music);
  43. PlayMusicStream(music);
  44. }
  45. // Pause/Resume music playing
  46. if (IsKeyPressed(KEY_P))
  47. {
  48. pause = !pause;
  49. if (pause) PauseMusicStream(music);
  50. else ResumeMusicStream(music);
  51. }
  52. // Get normalized time played for current music stream
  53. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music);
  54. if (timePlayed > 1.0f) timePlayed = 1.0f; // Make sure time played is no longer than music
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
  61. DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
  62. DrawRectangle(200, 200, (int)(timePlayed*400.0f), 12, MAROON);
  63. DrawRectangleLines(200, 200, 400, 12, GRAY);
  64. DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
  65. DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
  66. EndDrawing();
  67. //----------------------------------------------------------------------------------
  68. }
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. UnloadMusicStream(music); // Unload music stream buffers from RAM
  72. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  73. CloseWindow(); // Close window and OpenGL context
  74. //--------------------------------------------------------------------------------------
  75. return 0;
  76. }