Fix audio bug
This commit is contained in:
+18
-22
@@ -43,32 +43,27 @@ func Init(cfg *db.AppConfig, onUpdate func()) error {
|
||||
|
||||
refreshCacheCallback = onUpdate
|
||||
|
||||
// Let SDL choose the best audio driver for the system
|
||||
// Remove hardcoded ALSA preference to support PulseAudio, PipeWire, etc.
|
||||
// Users can set SDL_AUDIODRIVER environment variable if needed
|
||||
|
||||
if err := sdl.InitSubSystem(sdl.INIT_AUDIO); err != nil {
|
||||
return fmt.Errorf("SDL audio init failed: %w", err)
|
||||
}
|
||||
|
||||
// WAV-only does not require mix.Init(); add when you support OGG/MP3/etc.
|
||||
// FIX 1: Initialize MP3 and OGG decoders
|
||||
err := mix.Init(mix.INIT_MP3 | mix.INIT_OGG)
|
||||
if err != nil {
|
||||
logger.Error("Failed to initialize MP3/OGG support: %v", err)
|
||||
}
|
||||
|
||||
if err := openDeviceLocked(cfg.SelectedAudioDevice); err != nil {
|
||||
// Clean up the subsystem on failure so callers can retry later.
|
||||
sdl.QuitSubSystem(sdl.INIT_AUDIO)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Check if this logic is still needed; move to application
|
||||
// if lastVolume < 0 {
|
||||
// lastVolume = int(mix.MAX_VOLUME)
|
||||
// currentVolume = lastVolume
|
||||
// if err := db.SetConfigValue("last_volume", strconv.Itoa(lastVolume)); err != nil {
|
||||
// }
|
||||
// } else {
|
||||
// currentVolume = lastVolume
|
||||
// }
|
||||
|
||||
// mix.VolumeMusic(currentVolume)
|
||||
// FIX 2: Actually set a default volume!
|
||||
currentVolume = 128 // MAX_VOLUME
|
||||
if cfg.LastVolume > 0 {
|
||||
currentVolume = cfg.LastVolume
|
||||
}
|
||||
mix.VolumeMusic(currentVolume)
|
||||
|
||||
inited = true
|
||||
return nil
|
||||
@@ -109,9 +104,11 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error {
|
||||
defer mu.Unlock()
|
||||
|
||||
if !inited || !deviceOpen {
|
||||
logger.Error("audio mixer not initialized")
|
||||
return errors.New("audio mixer not initialized")
|
||||
}
|
||||
if path == "" {
|
||||
logger.Error("empty audio path")
|
||||
return errors.New("empty audio path")
|
||||
}
|
||||
|
||||
@@ -134,10 +131,7 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error {
|
||||
currentLoops = 0
|
||||
}
|
||||
|
||||
// Open the device
|
||||
if err := openDeviceLocked(deviceName); err != nil {
|
||||
logger.Error("Failed to open device: %w", err)
|
||||
}
|
||||
// DELETE the openDeviceLocked block that was here!
|
||||
|
||||
// Apply remembered volume
|
||||
mix.VolumeMusic(currentVolume)
|
||||
@@ -145,6 +139,7 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error {
|
||||
if err := currentMusic.Play(currentLoops); err != nil {
|
||||
currentMusic.Free()
|
||||
currentMusic = nil
|
||||
logger.Error("music.Play failed: %v", err)
|
||||
return fmt.Errorf("music.Play: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -331,8 +326,9 @@ func Shutdown() {
|
||||
mix.CloseAudio()
|
||||
deviceOpen = false
|
||||
}
|
||||
// If you add mix.Init(decoderFlags) in the future, pair it with mix.Quit() here.
|
||||
|
||||
if inited {
|
||||
mix.Quit() // Add this to clean up the decoders
|
||||
sdl.QuitSubSystem(sdl.INIT_AUDIO)
|
||||
inited = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user