Main
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javazoom.jl.decoder.JavaLayerException;
public class SoundPlayTest {
public static void main(String[] args) throws LineUnavailableException, IOException, JavaLayerException, UnsupportedAudioFileException {
RawPlayer rawPlayer = new RawPlayer();
rawPlayer.play(new FileInputStream(new File("sample.wav")));
Mp3Player mp3Player = new Mp3Player();
mp3Player.play(new FileInputStream(new File("sample.mp3")));
WavPlayer wavPlayer = new WavPlayer();
wavPlayer.play(new FileInputStream(new File("sample.wav")));
}
}
RAW
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class RawPlayer {
private byte buf[] = new byte[1024];
private int len;
public void play(InputStream rawfile) throws LineUnavailableException, FileNotFoundException, IOException {
BufferedInputStream raw = new BufferedInputStream(rawfile);
AudioFormat format = new AudioFormat(8000, 16, 1, true, false);
// select audio format parameters
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// prepare audio output
line.open(format, 1024);
line.start();
// play audio
while ((len = raw.read(buf)) != -1)
line.write(buf, 0, len);
// shut down audio
line.drain();
line.stop();
line.close();
}
}
MP3
import java.io.InputStream;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
import static java.lang.System.out;
public class Mp3Player {
public void play(InputStream mp3file) throws JavaLayerException {
AudioDevice device = FactoryRegistry.systemRegistry().createAudioDevice();
// create an MP3 player
AdvancedPlayer player = new AdvancedPlayer(mp3file, device);
player.setPlayBackListener(new PlaybackListener() {
@Override
public void playbackStarted(PlaybackEvent evt) {
out.println("[Playback] started.");
}
@Override
public void playbackFinished(PlaybackEvent evt) {
out.println("[Playback] finished.");
}
});
// play it!
player.play();
}
}
WAV
import java.io.IOException;
import java.io.InputStream;
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 WavPlayer {
private byte buf[] = new byte[1024];
private int len;
public void play(InputStream wavfile) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream wav = AudioSystem.getAudioInputStream(wavfile);
AudioFormat format = wav.getFormat();
// prepare audio output
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// prepare audio output
line.open(format, 1024);
line.start();
// play audio
while ((len = wav.read(buf)) != -1)
line.write(buf, 0, len);
// shut down audio
line.drain();
line.stop();
line.close();
}
}
I use JavaZOOM to play MP3s.
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1.4</version>
</dependency>
Recommended Posts