Play sound in game with SDL2

Hello,

What is the ideal procedure for playing a sound in a game developed with SDL2.

I use rodio to play a sound, but the problem is that the variable 's' is destroyed at the end of the scope of the variable, but the sound has not finished playing

One of the possibilities is to make a separate'thread '. But before I start I would like to have the opinion of another person to know the implementation they have made.

The sound is played when i press spacebar.


    let device = rodio::default_output_device().unwrap();

    let file = std::fs::File::open("examples/beep.wav").unwrap();
    let beep1 = rodio::play_once(&device, BufReader::new(file)).unwrap();
    beep1.set_volume(0.2);
    println!("Started beep1");

    thread::sleep(Duration::from_millis(1500));

    let file = std::fs::File::open("examples/beep2.wav").unwrap();
    rodio::play_once(&device, BufReader::new(file))
        .unwrap()
        .detach();
    println!("Started beep2");

    thread::sleep(Duration::from_millis(1500));

sample by radio

i find the solution

        let device = rodio::default_output_device().unwrap();

        let file = File::open("assets/sons/shoot.wav").unwrap();
        let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
        rodio::play_raw(&device, source.convert_samples());

run fine.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.