python ALSA:无法打开音频设备:没有这样的文件或目录[重复]

ohfgkhjo  于 2023-04-10  发布在  Python
关注(0)|答案(2)|浏览(761)

此问题已在此处有答案

Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."(1个答案)
昨天关门了。
如果我以root身份启动我的python程序(我必须以keyboard-module作为root原因启动它),这个问题就会出现:

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

如果我使用sudo aplay test.wav手动运行a sound,则会出现以下错误:

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

系统

/-                      
                   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
42fyovps

42fyovps1#

尝试给你的声音文件的完整路径。如:

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

如果这不起作用,您的SDL默认声音驱动程序是alsa。您可以使用以下命令更改声音驱动程序:

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

将这两行放在代码的最顶端。

hs1rzwqc

hs1rzwqc2#

必须使用pygame.init()初始化pygame才能使用混合器。

相关问题