pygame 尝试使用 alsa

pygame tries to use alsa

当我尝试 运行 一个使用 pygame 的程序时,我得到一个缓慢的 "startup"(init) (4-5s) 和这个来自 ALSA 的错误。修复 ALSA 不是我的 objective,我想知道如果我的代码中没有用到音频,为什么要初始化它。

$ python slides.py
aaaaaaaaaa
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4738:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default
bbbbbbbbbbb

这是消息 "conectando..." 之前 运行 的代码(抱歉,如果表格中断)

#!/usr/bin/python

import pygame

[...]
         print "aaaaaaaaaa"
         pygame.init()
         print "bbbbbbbbbbb"

所以基本上我只是初始化 pygame。这可能有什么问题?

如果我这样做也会发生这种情况

>>> from pygame import display, init as pyg_init
>>> pyg_init()
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4738:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default

这是导入 display 和 init 后加载的模块列表(以 "pygame" 开头)

pygame.transform
pygame.sndarray
pygame.numpy
pygame._arraysurfarray
pygame.fastevent
pygame.threads.traceback
pygame.version
pygame.surflock
pygame.image
pygame.color
pygame._numpysndarray
pygame.joystick
pygame.overlay
pygame.imageext
pygame.sprite
pygame.mask
pygame.threads.Queue
pygame.pygame
pygame.event
pygame.threads.sys
pygame.threads
pygame.threads.pygame
pygame.scrap
pygame.copy_reg
pygame.movie
pygame.mouse
pygame._numpysurfarray
pygame.pixelarray
pygame.surface
pygame.key
pygame.base
pygame.compat
pygame.sysfont
pygame.colordict
pygame.string
pygame.sys
pygame.re
pygame.cursors
pygame.cdrom
pygame.time
pygame.mixer
pygame.os
pygame.draw
pygame
pygame.rect
pygame.rwobject
pygame.mixer_music
pygame.constants
pygame.surfarray
pygame.threads.threading
pygame.font
pygame.bufferproxy
pygame.display

pygame.init() 尝试为您初始化所有 pygame 模块 (documentation)

如果您不希望这种情况发生(在您的情况下听起来很像),您必须自己初始化每个要使用的模块。每个模块都有一个 init 方法,您可以在使用该模块之前调用该方法。您还可以多次为每个模块调用 init 而不会产生任何副作用。

我最近来了 across 这个 post,因为我有一个类似的问题。 Pygame 在我安装它和它的模块时工作正常(我一直在学习图形)。然后我有了安装 Kivy 的想法,因为我一直想使用它,但像往常一样我无法在 Ubuntu 16.04(Windows APP 版本)上运行它。然后我卸载了它及其所有依赖项。当我 运行 一个 pygame 脚本时,系统提示我:

$ ./timer.py
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4259:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4738:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default

这促使我安装 ASLA 库并尝试关注 wiki。在修补并试图找出问题所在之后,我慢慢发现自己被阅读多个线程逼疯了,直到我来到 across jdonalds post: https://raspberrypi.stackexchange.com/questions/83254/pygame-and-alsa-lib-error。他的解决方案是通过在 os 模块中使用 os.environ 来指定不同的 SDL 默认声音驱动程序:

import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'

因为我有多个我不想更改的图形脚本,所以我将以下行添加到我的 .bashrc:

export SDL_AUDIODRIVER='dsp'

驱动程序可用性取决于平台和 SDL 编译时选项。您还可以在以下 link 了解有关 SDL 常见问题解答的更多信息:https://wiki.libsdl.org/FAQUsingSDL

在 colab 中使用 gym 时遇到了这个问题,通过注释掉 python env.render() 进行训练解决了。

在本地 windows 机器上(没有音频输出)插入我的耳机。