This is a streamlined code that can support very limited formats.
The code copy is as follows:
package com.hongyuan.test;
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;
public class MusicTest {
public static final String MUSIC_FILE = "Smile when we meet.wav";
public static void main(String[] args) throws LineUnavailableException,
UnsupportedAudioFileException, IOException {
// Get audio input stream
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new File(MUSIC_FILE));
// Get the audio encoding object
AudioFormat audioFormat = audioInputStream.getFormat();
// Set data input
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
audioFormat, AudioSystem.NOT_SPECIFIED);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
/*
* Read data from the input stream and send to the mixer
*/
int count;
byte tempBuffer[] = new byte[1024];
while ((count = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (count > 0) {
sourceDataLine.write(tempBuffer, 0, count);
}
}
// Clear the data buffer and close the input
sourceDataLine.drain();
sourceDataLine.close();
}
}