Search code examples
javaraspberry-piwavclip

Play wav file on Raspberry using Java


How can I reproduce a .wav file using Java on Raspberry (OS: Raspbian)? I tried this and it worked, but not anymore: now the program crashes when it tries to play the file (and I don't know why):

try{
   File adv = new File(pathRasp+"Suoni/avviso20.wav");
   Clip clip = AudioSystem.getClip();
   clip.open(AudioSystem.getAudioInputStream(adv));
   clip.start();
}catch(IOException | LineUnavailableException | UnsupportedAudioFileException ex){
   System.out.println("ERROR" + ex);
}

Anyone know any other ways to reproduce .wav files?


Solution

  • I found this blog post which may or may not help:

    https://nealvs.wordpress.com/2017/08/11/java-sound-on-a-raspberry-pi-with-openjdk/

    I was pretty bummed when my sound stopped playing on my Raspberry Pi when I switched to OpenJDK 8 after originally using Oracle JDK 8.

    Editing the sound.properties for the OpenJDK solved my problem.

    sudo vim /etc/java-8-openjdk/sound.properties

    Comment out the icedtea classpath configs:

    #javax.sound.sampled.Clip=org.classpath.icedtea.pulseaudio.PulseAudioMixerProvider #javax.sound.sampled.Port=org.classpath.icedtea.pulseaudio.PulseAudioMixerProvider #javax.sound.sampled.SourceDataLine=org.classpath.icedtea.pulseaudio.PulseAudioMixerProvider #javax.sound.sampled.TargetDataLine=org.classpath.icedtea.pulseaudio.PulseAudioMixerProvider

    Remove the comments from the sun classpath configs:

    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

    Problem solved.

    In other words, you might have a different JDK between your Windows and Raspi, and they need different configuration.