ALSA: Couldn't open audio device
Asked Answered
M

2

7

First of all, the file that I want to play is not the reason for the error; this looks like a problem with ALSA. As you can see, even when I want to start the sound file directly in the terminal (auto-complete) I am getting an error.

If I start my Python program as root (I have to start it as root cause the keyboard-module) this problem occurred:

ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
Stopping program
Stopping program
Traceback (most recent call last):
  File "[...]/run.py", line 26, in <module>
    main()
  File "[...]/run.py", line 11, in main
    GameStateManager.change_gamestate(SetupState())
  File "[...]/src/game_state.py", line 103, in __init__
    self.setup_sound_manager()
  File "[...]/src/game_state.py", line 110, in setup_sound_manager
    GameState.sound_manager = SoundManager()
  File "[...]/src/utils.py", line 253, in __init__
    mixer.init()
pygame.error: ALSA: Couldn't open audio device: No such file or directory

If I run a a sound manually with sudo aplay test.wav than this error comes up:

ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave

System

                    /-                      
                   ooo:                     ----------------------------
                  yoooo/                    OS: ArcoLinux
                 yooooooo                   Kernel: 5.17.4-arch1-1
                yooooooooo                  Uptime: 3 days, 23 hours, 2 mins
               yooooooooooo                 Packages: 1373 (pacman)
             .yooooooooooooo                Shell: zsh 5.8.1
            .oooooooooooooooo               Resolution: 1920x1080
           .oooooooarcoooooooo              WM: i3
          .ooooooooo-oooooooooo             Theme: Arc-Dark [GTK2/3]
         .ooooooooo-  oooooooooo            Icons: Sardi-Arc [GTK2/3]
        :ooooooooo.    :ooooooooo           Terminal: alacritty
       :ooooooooo.      :ooooooooo          CPU: AMD Ryzen 5 2500U with Radeon Vega Mobile Gfx (8) @ 2.000GHz
      :oooarcooo         .oooarcooo         GPU: AMD ATI Radeon Vega Series / Radeon Vega Mobile Series
     :ooooooooy           .ooooooooo        Memory: 2015MiB / 6904MiB (29%)
    :ooooooooo   /ooooooooooooooooooo
   :ooooooooo      .-ooooooooooooooooo.
  ooooooooo-             -ooooooooooooo.
 ooooooooo-                 .-oooooooooo.
ooooooooo.                     -ooooooooo

This question is totally unrelated to my problem, which is with the audio component specifically being unable to open the driver. Linked question is about a resource not loading.

Magel answered 28/4, 2022 at 11:25 Comment(0)
K
0

Try to give full path of your sound file. Such as:

your/full/path/to/file/test.wav

If this didn't work, Your SDL default sound driver is alsa. You can change your sound driver using this command:

import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'
# Your code

Put these two lines very top of your code.

Klingel answered 28/4, 2022 at 11:28 Comment(7)
Nope, does not work. The problem is, that alsa is not able to find the device, like a sound-card. If I did understood it correctlyMagel
Edited the answer. You can try changing your sound driver.Erlin
Second doesn't work either. mixer.init() -> pygame.errorMagel
Add pygame.init() command after importing pygame.Erlin
No. I just want to init the mixer. I do not need the other modules.Magel
You can use pygame.mixer.init() to only init the mixer module.Erlin
Yes, this is, what i am doing.Magel
S
-1

You MUST initialize pygame using pygame.init() to use the mixer.

Schizo answered 8/4, 2023 at 10:26 Comment(1)
Sorry for my late response. I did initialize the mixer. It semes like the problem is on the OS site. I did changed from ArcoLinux to Manjaro. Here i didn't had a problem like this.Magel

© 2022 - 2024 — McMap. All rights reserved.