Pause Java Audio during playback using command line?

ronierichard

New Member
I'm attempting to make a command line program that takes a file as an argument and plays that file.I have achieved this part as can be seen below but what I'm stuck on is trying to develop a way of pausing the file during playback. I tried messing around with different threads but I just couldn't get it to work properly. As this is being done in the command line I assume there is no way of using an \[code\]ActionListener\[/code\]?\[code\]import java.io.File;import java.io.IOException;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;public class SimpleAudioPlayer{ private static final int EXTERNAL_BUFFER_SIZE = 128000; public static void main(String[] args) { if (args.length != 1) { printUsageAndExit(); } String strFilename = args[0]; File soundFile = new File(strFilename); AudioInputStream audioInputStream = null; try { audioInputStream = AudioSystem.getAudioInputStream(soundFile); } catch (Exception e) { e.printStackTrace(); System.exit(1); } AudioFormat audioFormat = audioInputStream.getFormat(); SourceDataLine line = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat); try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } line.start(); int nBytesRead = 0; byte[] abData = http://stackoverflow.com/questions/15777103/new byte[EXTERNAL_BUFFER_SIZE]; while (nBytesRead != -1) { try { nBytesRead = audioInputStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { int nBytesWritten = line.write(abData, 0, nBytesRead); } } line.drain(); line.close(); System.exit(0); } private static void printUsageAndExit() { out("SimpleAudioPlayer: usage:"); out("\tjava SimpleAudioPlayer <soundfile>"); System.exit(1); } private static void out(String strMessage) { System.out.println(strMessage); }}\[/code\]
 
Back
Top