Here are the steps you should follow:
- Call
InitAudioDevice
to init audio device - call
LoadSound
to createSound
instance - call
LoadMusicStream
to createMusic
instance - Call
PlaySound
to play your sound effect - Call
PlayMusicStream, PauseMusicStream, StopMusicStream
to control your music, make sure to callUpdateMusicStream
in every frame logic to update music buffer - Optional, if you want to draw a music progress bar, then you need the following code to calculate the played time:
// Time played normalized [0.0f..1.0f] var timePlayed: f32 = 0.0; while (is_running) { // // Update music buffer with new stream data // rl.UpdateMusicStream(background_music); // // Get normalized time played for current music stream, used to update // progress bar // timePlayed = rl.GetMusicTimePlayed(background_music) / rl.GetMusicTimeLength(background_music); // Make sure time played is no longer than music if (timePlayed > 1.0) { timePlayed = 1.0; } rl.BeginDrawing(); // // Background music progress bar // rl.DrawRectangle( 200, // x 180, // y @as(c_int, @intFromFloat(timePlayed * 400.0)), // Played time width 12, // height TRON_ORANGE, ); rl.EndDrawing(); }
- Finally, call
UnloadMusicStream
,UnloadSound
andCloseAudioDevice
before program closes