audio_music_stream.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Music playing (streaming) (adapted for HTML5 platform)
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.7 (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. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. //----------------------------------------------------------------------------------
  18. // Global Variables Definition
  19. //----------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. int framesCounter = 0;
  23. float timePlayed = 0.0f;
  24. static bool pause = false;
  25. Music music;
  26. //----------------------------------------------------------------------------------
  27. // Module Functions Declaration
  28. //----------------------------------------------------------------------------------
  29. void UpdateDrawFrame(void); // Update and Draw one frame
  30. //----------------------------------------------------------------------------------
  31. // Main Enry Point
  32. //----------------------------------------------------------------------------------
  33. int main()
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
  38. InitAudioDevice(); // Initialize audio device
  39. music = LoadMusicStream("resources/guitar_noodling.ogg");
  40. PlayMusicStream(music);
  41. #if defined(PLATFORM_WEB)
  42. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  43. #else
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. UpdateDrawFrame();
  50. }
  51. #endif
  52. // De-Initialization
  53. //--------------------------------------------------------------------------------------
  54. UnloadMusicStream(music); // Unload music stream buffers from RAM
  55. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }
  60. //----------------------------------------------------------------------------------
  61. // Module Functions Definition
  62. //----------------------------------------------------------------------------------
  63. void UpdateDrawFrame(void)
  64. {
  65. // Update
  66. //----------------------------------------------------------------------------------
  67. UpdateMusicStream(music); // Update music buffer with new stream data
  68. // Restart music playing (stop and play)
  69. if (IsKeyPressed(KEY_SPACE))
  70. {
  71. StopMusicStream(music);
  72. PlayMusicStream(music);
  73. }
  74. // Pause/Resume music playing
  75. if (IsKeyPressed(KEY_P))
  76. {
  77. pause = !pause;
  78. if (pause) PauseMusicStream(music);
  79. else ResumeMusicStream(music);
  80. }
  81. // Get timePlayed scaled to bar dimensions (400 pixels)
  82. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
  83. //----------------------------------------------------------------------------------
  84. // Draw
  85. //----------------------------------------------------------------------------------
  86. BeginDrawing();
  87. ClearBackground(RAYWHITE);
  88. DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
  89. DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
  90. DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
  91. DrawRectangleLines(200, 200, 400, 12, GRAY);
  92. DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
  93. DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }