audio_music_stream.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.6 (www.raylib.com)
  8. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. --
  10. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  11. --
  12. -------------------------------------------------------------------------------------------
  13. -- Initialization
  14. -------------------------------------------------------------------------------------------
  15. local screenWidth = 800
  16. local screenHeight = 450
  17. InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)")
  18. InitAudioDevice() -- Initialize audio device
  19. local music = LoadMusicStream("resources/audio/guitar_noodling.ogg")
  20. PlayMusicStream(music)
  21. local framesCounter = 0
  22. local timePlayed = 0.0
  23. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  24. -------------------------------------------------------------------------------------------
  25. -- Main game loop
  26. while not WindowShouldClose() do -- Detect window close button or ESC key
  27. -- Update
  28. ---------------------------------------------------------------------------------------
  29. framesCounter = framesCounter + 1
  30. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*100*4 -- We scale by 4 to fit 400 pixels
  31. UpdateMusicStream(music) -- Update music buffer with new stream data
  32. ---------------------------------------------------------------------------------------
  33. -- Draw
  34. ---------------------------------------------------------------------------------------
  35. BeginDrawing()
  36. ClearBackground(RAYWHITE)
  37. DrawText("MUSIC SHOULD BE PLAYING!", 255, 200, 20, LIGHTGRAY)
  38. DrawRectangle(200, 250, 400, 12, LIGHTGRAY)
  39. DrawRectangle(200, 250, timePlayed//1, 12, MAROON)
  40. EndDrawing()
  41. ---------------------------------------------------------------------------------------
  42. end
  43. -- De-Initialization
  44. -------------------------------------------------------------------------------------------
  45. UnloadMusicStream(music) -- Unload music stream buffers from RAM
  46. CloseAudioDevice() -- Close audio device (music streaming is automatically stopped)
  47. CloseWindow() -- Close window and OpenGL context
  48. -------------------------------------------------------------------------------------------