audio_music_stream.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #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 stream");
  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. float pan = 0.0f; // Default audio pan center [-1.0f..1.0f]
  32. SetMusicPan(music, pan);
  33. float volume = 0.8f; // Default audio volume [0.0f..1.0f]
  34. SetMusicVolume(music, volume);
  35. SetTargetFPS(30); // Set our game to run at 30 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. UpdateMusicStream(music); // Update music buffer with new stream data
  43. // Restart music playing (stop and play)
  44. if (IsKeyPressed(KEY_SPACE))
  45. {
  46. StopMusicStream(music);
  47. PlayMusicStream(music);
  48. }
  49. // Pause/Resume music playing
  50. if (IsKeyPressed(KEY_P))
  51. {
  52. pause = !pause;
  53. if (pause) PauseMusicStream(music);
  54. else ResumeMusicStream(music);
  55. }
  56. // Set audio pan
  57. if (IsKeyDown(KEY_LEFT))
  58. {
  59. pan -= 0.05f;
  60. if (pan < -1.0f) pan = -1.0f;
  61. SetMusicPan(music, pan);
  62. }
  63. else if (IsKeyDown(KEY_RIGHT))
  64. {
  65. pan += 0.05f;
  66. if (pan > 1.0f) pan = 1.0f;
  67. SetMusicPan(music, pan);
  68. }
  69. // Set audio volume
  70. if (IsKeyDown(KEY_DOWN))
  71. {
  72. volume -= 0.05f;
  73. if (volume < 0.0f) volume = 0.0f;
  74. SetMusicVolume(music, volume);
  75. }
  76. else if (IsKeyDown(KEY_UP))
  77. {
  78. volume += 0.05f;
  79. if (volume > 1.0f) volume = 1.0f;
  80. SetMusicVolume(music, volume);
  81. }
  82. // Get normalized time played for current music stream
  83. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music);
  84. if (timePlayed > 1.0f) timePlayed = 1.0f; // Make sure time played is no longer than music
  85. //----------------------------------------------------------------------------------
  86. // Draw
  87. //----------------------------------------------------------------------------------
  88. BeginDrawing();
  89. ClearBackground(RAYWHITE);
  90. DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
  91. DrawText("LEFT-RIGHT for PAN CONTROL", 320, 74, 10, DARKBLUE);
  92. DrawRectangle(300, 100, 200, 12, LIGHTGRAY);
  93. DrawRectangleLines(300, 100, 200, 12, GRAY);
  94. DrawRectangle(300 + (pan + 1.0)/2.0f*200 - 5, 92, 10, 28, DARKGRAY);
  95. DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
  96. DrawRectangle(200, 200, (int)(timePlayed*400.0f), 12, MAROON);
  97. DrawRectangleLines(200, 200, 400, 12, GRAY);
  98. DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
  99. DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
  100. DrawText("UP-DOWN for VOLUME CONTROL", 320, 334, 10, DARKGREEN);
  101. DrawRectangle(300, 360, 200, 12, LIGHTGRAY);
  102. DrawRectangleLines(300, 360, 200, 12, GRAY);
  103. DrawRectangle(300 + volume*200 - 5, 352, 10, 28, DARKGRAY);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. UnloadMusicStream(music); // Unload music stream buffers from RAM
  110. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  114. }