How to select audio output device?

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: Openhabian 2.2.0-1
    • Java Runtime Environment: openjdk version “1.8.0_152”

Hello,

I have a USB audio device plugged in and working as a default audio device, ie.

aplay soundfile.wav

works and plays audio through the USB speaker.

USB audio as first audio device:

openhabian@openHABianPi:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: CX100 [Polycom CX100], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Problem is that OH still plays audio through the 3.5mm jack. Anyone know how to change that?

I think java tends to default to using the default hardware audio device. You need to add the following to the java sound.properties configuration file which makes it use the default ALSA audio device:

javax.sound.sampled.Clip=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.Port=com.sun.media.sound.PortMixerProvider
javax.sound.sampled.SourceDataLine=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.TargetDataLine=com.sun.media.sound.DirectAudioDeviceProvider

The configuration file on a raspberry pi can usually be found in /usr/lib/jvm/java version/jre/lib

You will need to restart OH after changing the file.

3 Likes

And it works! That’s pretty amazing, I went through a lot of threads but couldn’t find this answer anywhere, awesome :slight_smile:

Hello MikeJMajor, hello autoit,

i have the same problem with my audio device. Could you please tell me the steps, how i ad the config file, because i´m not so the linux crack.
Many thanks

Frank

https://www.google.com/search?q=asound.conf+raspberry+pi

Hello luckymallari,

thanks for the google search links.
The audio out works fine under linux, i tested it with “aplay” and “speaker-test …“
The problem is, under openhab there is no audio output for example …say(””).

Like MikeJMajor said above, just add the code to file /usr/lib/jvm/java version/jre/lib/sound.properties and reboot HA.

yeah works! I commented the first set of lines to disable them and then enabled the lines you had above and now sound is playing after a reboot.

Not working for me. My Java installs don’t have JRE folders. I tried placing in lib folder but that did not work. Still sound heading to HDMI monitor that has no speakers. I then found a file in /etc/java-11-openjdk/sound.properties and modified it. But still no luck.

Raspberry Pi 4B, latest OS

Default sound from other apps is AV Jack. But OH3 keeps sending to HDMI.

Any other ideas?

I got it working:
using Setting the default device - AlsaProject

This seems to set the default for Java too.

Hi! I have simple USB DAC and it works well when it comes to run commands from RPI4 console, like aplay or mpg123.
It doesn’t work however with OH3.3 (M4) command playSound that I use in rules. Perhaps OH via Java doesn’t use appropriate sound device.
Any ideas?

EDIT:
Ok, worked with the tip from @MikeJMajor. I guess this mod could be part of openhabian’s menu.

Click the small speaker icon next to the clock on your Windows taskbar. Select the small up arrow to the right of your current audio output device. Select your output of choice from the list that appears.

Running OH3 on RPI3B
Not able to locate /usr/lib/jvm/java version/jre/lib
Can you share where you made the edits
Much Appreciated