Table of Contents
Question content
Solution
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

Feb 09, 2024 am 09:24 AM

无法使用 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

See all articles