File audioFile = new File(audioFilePath); AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.open(audioStream); audioClip.start();
audioClip.close(); audioStream.close();
package net.codejava.sound; 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.Clip; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineListener; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; /** * This is an example program that demonstrates how to play back an audio file * using the Clip in Java Sound API. * @author www.codejava.net * */ public class AudioPlayerExample1 implements LineListener { /** * this flag indicates whether the playback completes or not. */ boolean playCompleted; /** * Play a given audio file. * @param audioFilePath Path of the audio file. */ void play(String audioFilePath) { File audioFile = new File(audioFilePath); try { AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); AudioFormat format = audioStream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); Clip audioClip = (Clip) AudioSystem.getLine(info); audioClip.addLineListener(this); audioClip.open(audioStream); audioClip.start(); while (!playCompleted) { // wait for the playback completes try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } audioClip.close(); } catch (UnsupportedAudioFileException ex) { System.out.println("The specified audio file is not supported."); ex.printStackTrace(); } catch (LineUnavailableException ex) { System.out.println("Audio line for playing back is unavailable."); ex.printStackTrace(); } catch (IOException ex) { System.out.println("Error playing the audio file."); ex.printStackTrace(); } } /** * Listens to the START and STOP events of the audio line. */ @Override public void update(LineEvent event) { LineEvent.Type type = event.getType(); if (type == LineEvent.Type.START) { System.out.println("Playback started."); } else if (type == LineEvent.Type.STOP) { playCompleted = true; System.out.println("Playback completed."); } } public static void main(String[] args) { String audioFilePath = "E:/Test/Audio.wav"; AudioPlayerExample1 player = new AudioPlayerExample1(); player.play(audioFilePath); } }In this program, the play() method implements all the above mentioned steps, and the current thread will wait till the playback completes. We make this class implements the LineListener interface in order to receive line events (OPEN, CLOSE, START and STOP) to know when the playback completes (a STOP event is fired). Specify a listener for the Clip as follows:
audioClip.addLineListener(this);Where this is the current class that implements the LineListener interface:
public class AudioPlayerExample1 implements LineListenerAnd we have to override the update() method defined in the LineListener interface as follows:
@Override public void update(LineEvent event) { LineEvent.Type type = event.getType(); if (type == LineEvent.Type.START) { System.out.println("Playback started."); } else if (type == LineEvent.Type.STOP) { playCompleted = true; System.out.println("Playback completed."); } }If the line event type is STOP, we set the flag playCompletedto true, so the current thread stops waiting and the program exits.In addition, we can do the following things with the Clip:
int frameLength = audioClip.getFrameLength(); // length in frames long duration = audioClip.getMicrosecondLength(); // length in microsecondsYou need to divide the duration in microseconds by one million to get the value in seconds.
audioClip.setMicrosecondPosition(50_000_000); // start playing from the 50th second audioClip.setFramePosition(300_000); // start playing from the 300,000th frame
audioClip.loop(2); // loop 2 times (total play 3 times)
// loop from frame 10,000th to frame 500,000th audioClip.setLoopPoints(10_000, 500_000); audioClip.loop(1);
audioClip.stop();
To resume playing, call start() method again.
And all the above statements (except the stop()) should be called after audioClip.open() and before audioClip.start(), for example:audioClip.open(audioStream); int frameLength = audioClip.getFrameLength(); System.out.println("Frame length = " + frameLength); long duration = audioClip.getMicrosecondLength(); System.out.println("Duration = " + (duration / 1_000_000) + " sec"); audioClip.setMicrosecondPosition(10_000_000); // start playing from the 10th second audioClip.loop(1); // loop 1 time audioClip.start();
File audioFile = new File(audioFilePath); AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); AudioFormat format = audioStream.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info);
audioLine.open(format); audioLine.start();
int BUFFER_SIZE = 4096; byte[] bytesBuffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = audioStream.read(bytesBuffer)) != -1) { audioLine.write(bytesBuffer, 0, bytesRead); }
audioLine.drain(); audioLine.close(); audioStream.close();Java Playback Audio Example using DataSourceLine:Here’s an example program that plays back a given audio file using the SourceDataLine:
package net.codejava.sound; 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; import javax.sound.sampled.UnsupportedAudioFileException; /** * This is an example program that demonstrates how to play back an audio file * using the SourceDataLine in Java Sound API. * @author www.codejava.net * */ public class AudioPlayerExample2 { // size of the byte buffer used to read/write the audio stream private static final int BUFFER_SIZE = 4096; /** * Play a given audio file. * @param audioFilePath Path of the audio file. */ void play(String audioFilePath) { File audioFile = new File(audioFilePath); try { AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); AudioFormat format = audioStream.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info); audioLine.open(format); audioLine.start(); System.out.println("Playback started."); byte[] bytesBuffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = audioStream.read(bytesBuffer)) != -1) { audioLine.write(bytesBuffer, 0, bytesRead); } audioLine.drain(); audioLine.close(); audioStream.close(); System.out.println("Playback completed."); } catch (UnsupportedAudioFileException ex) { System.out.println("The specified audio file is not supported."); ex.printStackTrace(); } catch (LineUnavailableException ex) { System.out.println("Audio line for playing back is unavailable."); ex.printStackTrace(); } catch (IOException ex) { System.out.println("Error playing the audio file."); ex.printStackTrace(); } } public static void main(String[] args) { String audioFilePath = "E:/Test/Audio.wav"; AudioPlayerExample1 player = new AudioPlayerExample1(); player.play(audioFilePath); } }As we can see, this program is simpler than the Clip-based version, without having to implement the LineListener interface.