Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

98 linhas
3.7 KiB

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