6

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.

7
  • Did you try running this on say Windows? You're making it specific about raspberry-pi, but that would be a red herring if the code doesn't work somewhere else either. Commented May 21, 2015 at 15:20
  • 1
    Yes i have tried it on Windows and Linux and it works on both. I think java has some kind of problem noticing the audio output Commented May 21, 2015 at 16:17
  • You should edit your question and add the fact that the code is tested and working on Windows and Linux; its a rather important clue that it indeed may be raspberry-pi specific. When you run java -version on the PI, what does that output? Commented May 21, 2015 at 16:40
  • java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode) Commented May 21, 2015 at 16:41
  • This may be unrelated because the OP is using mp3 and JLayer, but it seems like it could help: raspberrypi.stackexchange.com/questions/1337/… Commented May 21, 2015 at 17:58

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is correct but what if we have selected an external USB sound card as default audio device? In my case, it works to record audio (as it is the only recording device detected by the system) but while aplay reproduces audio with it (I have configured it in asound.conf) Java sticks to internal jack audio, which is much worse quality as any cheap USB audio card. Any clues on how to make JDK use the selected audio output device?
Did your card work fine on Raspbian?, for example, by recording and playing directly with the tools of the system?
Yes, it works with any Alsa commands. Anyway, I have solved it forgetting about uSB microphone and using a simple old USB sound card with audio input output. Works great and the recording quality is much better Than USB micro, while also shows no problem with 16000 rate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.