Skip to content

Latest commit

 

History

History
63 lines (43 loc) · 1.7 KB

e-how-to-play-audio.org

File metadata and controls

63 lines (43 loc) · 1.7 KB

How to play audio

Regular steps to load and play sound/music

Here are the steps you should follow:

  • Call InitAudioDevice to init audio device
  • call LoadSound to create Sound instance
  • call LoadMusicStream to create Music instance
  • Call PlaySound to play your sound effect
  • Call PlayMusicStream, PauseMusicStream, StopMusicStream to control your music, make sure to call UpdateMusicStream 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 and CloseAudioDevice before program closes

Example

src/tutorial_examples/how-to-play-audio.zig