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.

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".

  • 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 8 '17 at 0: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 8 '17 at 0: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 8 '17 at 0:49
  • 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 8 '17 at 1: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 8 '17 at 1:09
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.