Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing audio on Raspberry pi with java

Hi i have this code here

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Playmusic implements Runnable {
    public static void main(String[] args){
        Thread t = new Thread(new Playmusic());
        t.start();
    }   

    @Override
    public void run() {
        AudioInputStream audioIn;
        try {
            audioIn = AudioSystem.getAudioInputStream(new File("test.wav"));
            Clip clip;
            clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
            Thread.sleep(clip.getMicrosecondLength()/1000);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException  e1) {
            e1.printStackTrace();
        }
    }
}

to play a sound on the raspberry. But when i run it, it doesn't produce any output.
I've tested it on both Windows and Linux systems where it works.
The program does notice the file though since it sleeps for the whole duration of the sound and doesn't give me any Runtime exception.
It also can't be the speaker that's causing the problem because i can play the sound with
aplay test.wav and it gives me an output. I wanted to use the JavaFX library but it seems to be removed on the cut down java version of resbian.

like image 743
Thomas Fellner Avatar asked May 21 '15 14:05

Thomas Fellner


1 Answers

This has nothing to do with Java or Raspbian... Check the RPi configuration sudo raspi-config, and ensure you are having your audio output well configured between HDMI or Jack Out. That should do the trick...

like image 165
Brethlosze Avatar answered Nov 06 '22 08:11

Brethlosze