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

huangapple go评论100阅读模式
英文:

Cannot initialize music using SDL2_Mixer, reports unrecognized audio format

问题

我的应用程序使用SDL2将天气数据渲染到窗口中,并且我想从生成的队列中无限播放一些背景音乐。我已经完成了这部分,但是加载音乐的时候出现了问题。每次我尝试在队列中加载一首歌曲时,它都会显示未识别的音频格式。以下是我的代码。

在初始化SDL音频和视频时,第277至280行。工作正常。

if err := sdl.Init(sdl.INIT_AUDIO | sdl.INIT_VIDEO); err != nil {
    log.Fatalf("Failed to initialize SDL: %s", err)
}
defer sdl.Quit()

在初始化混音器时,第282至285行。也工作正常。

if err := mix.Init(mix.INIT_FLAC | mix.INIT_OGG); err != nil {
    log.Fatalf("Failed to initialize mixer: %s", err)
}
defer mix.Quit()

在填充音乐队列时,第292至305行。也工作正常。

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
}

紧接着,在第307至314行,当我尝试加载队列中的每个文件时,问题就出现了。

for _, file := range music {
    music, err := mix.LoadMUS(file)
    if err != nil {
        log.Fatalf("Failed to load music: %s", err)
        continue
    }
    defer music.Free()
}

当我在程序中使用上述代码时,Go会打印出以下内容。

2023/03/30 19:57:18 Failed to load music: Unrecognized audio format

我正在尝试加载从FFMPEG转换的FLAC文件。我还尝试使用MP3文件和WAV文件(通过更改初始化标志和assets中的文件为可工作的对应文件),但它们也不起作用。

如果有帮助的话,我使用的操作系统是Windows 11,并且我使用MSYS2来使Go的SDL模块能够利用SDL2库。我还使用以下命令编译我的代码。

go build -ldflags="-s -w" -gcflags="-trimpath=$PWD" -race main.go
英文:

My app utilizes SDL2 to render weather data to a window, and I would like to play some music in the background indefinitely from a generated queue. I have that part done, but loading music is where the issue occurs. Every time I try to LoadMUS a song within the queue it states unrecognized audio format. This is what my code looks like.

Lines 277 - 280, when I initialize 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()

Lines 282 to 285, when I init the mixer. Works fine as well.

if err := mix.Init(mix.INIT_FLAC | mix.INIT_OGG); err != nil {
    log.Fatalf("Failed to initialize mixer: %s", err)
}
defer mix.Quit()

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
}

Shortly after, Lines 307 - 314, when I try to LoadMUS every file in the queue. This is where the issues arise.

for _, file := range music {
    music, err := mix.LoadMUS(file)
    if err != nil {
        log.Fatalf("Failed to load music: %s", err)
        continue
    }
    defer music.Free()
}

When I have the code above in my program, Go prints this.

2023/03/30 19:57:18 Failed to load music: Unrecognized audio format

I am trying to load FLAC files converted from FFMPEG. I've also attempted to use MP3 files and WAV files (by changing the init flags and files within assets to their working counterparts) but they don't work as well.

If it helps, the OS I am using is Windows 11, and I am using MSYS2 to for Go's SDL module to be able to utilize the SDL2 libraries. I also compile my code using the following command.

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

答案1

得分: 2

在加载声音之前,您需要打开音频设备。使用适当的参数调用mix.OpenAudiomix.OpenAudioDevice,例如:

mix.OpenAudio(48000, sdl.AUDIO_S16, 2, 4096);
英文:

You need to open audio device before loading sounds. Call mix.OpenAudio or mix.OpenAudioDevice with appropriate parameters, e.g.

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

huangapple
  • 本文由 发表于 2023年3月31日 07:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75893801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定