2

On a clean install of Raspbian Jessie I want to play mp3s via mplayer through my Sabrent USB Audio driver. On Raspbian itself this was plug n' play; I changed the default audio device to "USB Audio Device" and then Youtube etc. worked as desired. In the CLI,

mplayer myfile.mp3

also worked perfectly. But in Python (3.4), the audio keeps funneling through HDMI to my monitor's speakers. This happens even when I switch my Python mp3 player to pygame, so it seems to be an issue with RasPi settings / Python.

Edit: When I changed the default audio device, I did so through the Jessie desktop interface in Preferences / Audio Device Settings. That window lists "USB Audio Device (Alsa mixer)" as default, as desired.

Twiffy
  • 141
  • 1
  • 1
  • 6

2 Answers2

2

As pygame rely on SDL system, we have to define the audio driver for SDL. This can be done with environnement variables.

You can define it in your python script, but this must be done before you init pygame.

os.putenv('SDL_AUDIODRIVER', 'alsa')
os.putenv('SDL_AUDIODEV', '/dev/audio')

You may also define the env.var. SDL_AUDIODEV in the same manner, if you know the /dev/ node of your sound card. The value of this variable should look like "/dev/audio" "/dev/snd".

Technico.top
  • 1,406
  • 13
  • 20
  • That didn't work, but I think it's leading in the right direction. I'm running my python scripts as root, because they also use the RPi.GPIO library. Apparently running as root ignores the environment variables set by other users. So presumably I just need to figure out how to set environment variables for the root user. I tried "sudo nano /etc/environment" and adding "SDL_AUDIODRIVER = alsa", but that didn't work. – Twiffy Jun 08 '17 at 00:02
  • I also use pygame as root and can confirm that environnement variable, set by python, are working flawlessly. On your code, did you run pygame.mixer.init() ? Edited the question with another env. var. too. – Technico.top Jun 08 '17 at 00:14
  • Yes, both mplayer and pygame are still playing through HDMI when I run the script with sudo (and not otherwise). I wrote a script in /etc/profile.d to set the environment variable more globally, which worked to set the variable, but still the problem persists. I do run pygame.mixer.init(). Will try to find the /dev/ node and try the SDL_AUDIODEV variable. – Twiffy Jun 08 '17 at 00:49
  • 1
    Nope, didn't work. Used cat /proc/asound/cards to get hardware codes, then used export SDL_AUDIODEV=hw:1,0 to set priority, but problem persisted. Seems to be independent of python -- from CLI just tried sudo mplayer myfile.mp3 and it comes through HDMI. – Twiffy Jun 08 '17 at 01:00
  • About environnement variables, you can use the python command print(os.getenv('SDL_AUDIODRIVER')) and print(os.getenv('SDL_AUDIODEV'))to verify that these variables are correct on the program context. – Technico.top Jun 08 '17 at 01:09
  • Thanks! Finally found a solution. Setting the parameters in python never worked for me, but if it weren't for you talking about setting environment variables, I might never have gotten there. Much appreciated. – Twiffy Jun 08 '17 at 05:31
  • This didn't work for me, but the other comment about /etc/asound.conf worked great! – Nathan Garabedian Oct 31 '20 at 17:12
2

The problem wasn't anything to do with Python, but rather that my pi user settings were different from my root user settings, so that the former went through the chosen USB audio device, but I couldn't get the latter to change from default settings. I only noticed this via Python because I would run my python script using sudo.

The non-accepted answer from this thread did the trick! To recap here:

1) Get a list of your sound cards using aplay -l

2) Create/edit the system-wide alsa configuration file at /etc/asound.conf, e.g. with sudo nano /etc/asound.conf

3) Into this file, paste

pcm.!default {
    type hw
    card 0
}

ctl.!default {
    type hw           
    card 0
}

except use whatever the desired card number is. In my case this was card 1.

Twiffy
  • 141
  • 1
  • 1
  • 6
  • So helpful! Thank you so much, this allowed pygame to output to the headphone jack instead of the hdmi output (with card 1 in my case). I had already tried /etc/config.txt, configuring through raspi-config, everything else was not working. – Nathan Garabedian Oct 31 '20 at 17:10