Play RAW, WAV, MP3 files in Java

How to play audio files (RAW, WAV, MP3) in Java 8

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

Play RAW, WAV, MP3 files in Java
Read binary files in Java 1
Read binary files in Java 2
Easily read text files in Java (Java 11 & Java 7)
Play with Markdown in Java flexmark-java
Play Framework 2.6 (Java) environment construction in Eclipse
Convert SVG files to PNG files in Java
Reading and writing gzip files in Java
Partization in Java
Changes in Java 11
Pi in Java
Import files of the same hierarchy in Java
FizzBuzz in Java
How to input / output IBM mainframe files in Java?
Android-Upload image files to Azure Blob Storage in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Play Framework2.5 (Java) Tips
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Play non-XML valid characters in Java by specifying code values
Read WAV data as a byte array in Android Java
How to play MIDI files using the Java Sound API