Home > Backend Development > Golang > Unable to initialize music using SDL2_Mixer, reporting unrecognized audio format

Unable to initialize music using SDL2_Mixer, reporting unrecognized audio format

WBOY
Release: 2024-02-09 09:24:29
forward
776 people have browsed it

无法使用 SDL2_Mixer 初始化音乐,报告无法识别的音频格式

php Editor Banana encountered a problem when using SDL2_Mixer to initialize music, and the report showed an unrecognized audio format. This problem may affect the music playback function and cause inconvenience to users. In order to solve this problem, we need to find the cause of the unrecognized audio format and take appropriate measures to fix it. The following will introduce some possible causes and solutions, hoping to help developers who encounter similar problems.

Question content

My application utilizes sdl2 to render weather data to a window, and I want to play some music in the background indefinitely from the resulting queue. I've done that part, but loading the music is where the problem occurs. Every time I try to load a song in the queue it says unrecognized audio format. This is what my code looks like.

Lines 277 - 280, when I initialize the sdl for audio and video. Works fine.

if err := sdl.init(sdl.init_audio | sdl.init_video); err != nil {
    log.fatalf("failed to initialize sdl: %s", err)
}
defer sdl.quit()
Copy after login

Lines 282 to 285, when I initialize the mixer. The effect is also very good.

if err := mix.init(mix.init_flac | mix.init_ogg); err != nil {
    log.fatalf("failed to initialize mixer: %s", err)
}
defer mix.quit()
Copy after login

Lines 292 - 305 when I populate the music queue. Also works fine.

var music []string

err = filepath.walk("assets/music", func(path string, info os.fileinfo, err error) error {
    if err != nil {
        return err
    }
    if !info.isdir() && (filepath.ext(path) == ".flac" || filepath.ext(path) == ".ogg") {
        music = append(music, path)
    }
    return nil
})

if err != nil {
    log.fatalf("failed to queue music: %s", err)
    return
}
Copy after login

Shortly after, lines 307 - 314, when I try to loadmus every file in the queue. This is where the problem arises.

for _, file := range music {
    music, err := mix.loadmus(file)
    if err != nil {
        log.fatalf("failed to load music: %s", err)
        continue
    }
    defer music.free()
}
Copy after login

When I have the above code in my program, go will print this code.

2023/03/30 19:57:18 failed to load music: unrecognized audio format
Copy after login

I'm trying to load a flac file converted from ffmpeg. I also tried using mp3 files and wav files (by changing the initialization flags and files in the assets to their working counterparts) but they didn't work well.

If it helps, the operating system I'm using is windows 11, and I'm using msys2 to enable go's sdl module to utilize the sdl2 library. I also use the following command to compile my code.

go build -ldflags="-s -w" -gcflags="-trimpath=$PWD" -race main.go
Copy after login

Solution

You need to open the audio device before loading sounds. Call mix.openaudio or mix.openaudiodevice with appropriate parameters, e.g.

mix.OpenAudio(48000, sdl.AUDIO_S16, 2, 4096);
Copy after login

The above is the detailed content of Unable to initialize music using SDL2_Mixer, reporting unrecognized audio format. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template