I would like to play a sound file using java through the command line.
so far I have succeded at listing the contents of a directory, but cannot play a wav file
code to list the content of a directory
Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","ls"});
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = p.waitFor();
System.out.println("Exited with error code "+exitVal);
code to play a wav file, this doesn't work
Process p = Runtime.getRuntime().exec(new String[]{"/usr/bin/aplay","~/javafx/examples/PrayerTime/dist/police_s.wav"});
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = p.waitFor();
System.out.println("Exited with error code "+exitVal);
I get the following error
Exited with error code 1
Here is a slight variation of the code seen in the info. page for the javasound tag.
import java.net.URL;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
Thread.sleep(5000);
}
}
It works from the command line.