Skip to content

Loading sound and music directly from memory #1482

Open
@kaphula

Description

@kaphula

How do you load sound effects and music directly from memory the right way?

I am trying to create a chunk from a memory loaded sound file, .wav in this case, but it could .ogg as well.

let chunk = sdl2::mixer::Chunk::from_raw_buffer(up.raw_data.clone().into_boxed_slice()).unwrap();

But it does not work, when I play the sound all I hear is noise. I assume I need for SDL2 to recognize the loaded audio type and load it accordingly, but Chunk::from_raw_buffer does not do any of that work, as hinted by its documentation.

Does the API offer a safe way to load sounds as chunks from memory?

EDIT:

I managed to get it to work by writing the in-memory file back to disk as a file, then reading that file using the API, but that does not sound very smart. Is this the only way?

      let temp_path = std::env::temp_dir().join("temp_audio_file");
      std::fs::File::create(&temp_path)?.write_all(&up.raw_data)?;
      let chunk = sdl2::mixer::Chunk::from_file(&temp_path)?;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions