Created
March 27, 2014 19:04
-
-
Save anonymous/9815494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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