Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9815494 to your computer and use it in GitHub Desktop.
Save anonymous/9815494 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;
public class SimpleAudioPlayer {
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.out.println("SimpleAudioPlayer: usage:");
System.out.println("\tjava SimpleAudioPlayer <soundfile>");
System.exit(1);
}
playFile(new File(args[0]));
}
public static void playFile(File soundFile) throws Exception {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)
line.open(audioFormat);
line.start();
byte[] buffer = new byte[128000];
while(true) {
int read = audioInputStream.read(buffer, 0, buffer.length);
if(read < 0) break;
line.write(buffer, 0, read);
}
line.drain();
line.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment