Skip to content
Advertisement

Why python tries to use directly Alsa as a sound output?

I’m using Ubuntu 20.04.5 LTS

When I try to play sound with pygame or pyglet, it plays it normally. However, when my computer is playing sound from any other source, I just get error

Here is the pygame version

from pygame import mixer

mixer.init()
mixer.music.load("path_to_file.wav")
mixer.music.play()

I get an error:

pygame.error: ALSA: Couldn't open audio device: Device or resource busy

It seems that pygame tries to play audio directly from ALSA and that’s why I cannot play multiple sounds

I also tried pyglet

import pyglet
pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')

pyglet.media.load("path_to_file.wav", streaming=True).play()

This also works fine, unless there is audio coming from another source. Then I get this error:

AttributeError: 'OpenALDriver' object has no attribute 'worker'

Main problem here seems to be that these programs tries to use Alsa directly. I have installed pulseaudio and openal but for some reason it still cannot really use them. I think there is something missing from my system but I have no idea what can it be.

EDIT: I tried to change audiodriver via

from os import environ
environ["SDL_AUDIODRIVER"] = "pulseaudio"

But I get an error:

pygame.error: Could not setup connection to PulseAudio

Advertisement

Answer

Well I think I found the solution. Pygame couldn’t connect to other audio device or driver because I used sudo python3 the_file.py instead of python3 the_file.py. Moral of the story: Don’t use sudo on python projects.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement