audio_music_stream.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Music playing (streaming)
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.3 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
  21. InitAudioDevice(); // Initialize audio device
  22. PlayMusicStream("resources/audio/guitar_noodling.ogg"); // Play music stream
  23. int framesCounter = 0;
  24. float timePlayed = 0;
  25. //float volume = 1.0;
  26. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  27. //--------------------------------------------------------------------------------------
  28. // Main game loop
  29. while (!WindowShouldClose()) // Detect window close button or ESC key
  30. {
  31. // Update
  32. //----------------------------------------------------------------------------------
  33. framesCounter++;
  34. // Testing music fading from one file to another
  35. /*
  36. if (framesCounter > 600) // Wait for 10 seconds (600 frames)
  37. {
  38. volume -= 0.01; // Decrement music volume level
  39. // When music volume level equal or lower than 0,
  40. // restore volume level and init another music file
  41. if (volume <= 0)
  42. {
  43. volume = 1.0;
  44. framesCounter = 0;
  45. PlayMusicStream("resources/audio/another_file.ogg");
  46. }
  47. SetMusicVolume(volume);
  48. }
  49. */
  50. if (IsWindowMinimized()) PauseMusicStream();
  51. else ResumeMusicStream();
  52. timePlayed = GetMusicTimePlayed()/GetMusicTimeLength()*100*4; // We scale by 4 to fit 400 pixels
  53. UpdateMusicStream(); // Update music buffer with new stream data
  54. //----------------------------------------------------------------------------------
  55. // Draw
  56. //----------------------------------------------------------------------------------
  57. BeginDrawing();
  58. ClearBackground(RAYWHITE);
  59. DrawText("MUSIC SHOULD BE PLAYING!", 255, 200, 20, LIGHTGRAY);
  60. DrawRectangle(200, 250, 400, 12, LIGHTGRAY);
  61. DrawRectangle(200, 250, (int)timePlayed, 12, MAROON);
  62. EndDrawing();
  63. //----------------------------------------------------------------------------------
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  68. CloseWindow(); // Close window and OpenGL context
  69. //--------------------------------------------------------------------------------------
  70. return 0;
  71. }