Spielen Sie RAW-, WAV- und MP3-Dateien in Java ab

So spielen Sie Audiodateien (RAW, WAV, MP3) auf Java 8 ab

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();
	}

}

JavaZOOM wird zum Abspielen von MP3 verwendet.

		<dependency>
			<groupId>com.googlecode.soundlibs</groupId>
			<artifactId>jlayer</artifactId>
			<version>1.0.1.4</version>
		</dependency>

Recommended Posts

Spielen Sie RAW-, WAV- und MP3-Dateien in Java ab
Lesen Sie Binärdateien in Java 1
Lesen Sie Binärdateien in Java 2
Einfaches Lesen von Textdateien in Java (Java 11 & Java 7)
Tweak Markdown mit Java Flexmark-Java
Spielen Sie die Framework 2.6 (Java) -Umgebungskonstruktion mit Eclipse
Konvertieren Sie SVG-Dateien in Java in PNG-Dateien
Lesen und Schreiben von GZIP-Dateien in Java
Partisierung in Java
Änderungen in Java 11
Umfangsrate in Java
Importieren Sie Dateien derselben Hierarchie in Java
FizzBuzz in Java
Wie kann ich IBM Mainframe-Dateien in Java eingeben / ausgeben?
Android-Laden Sie Bilddateien in den Azure Blob-Speicher in Java hoch
Lesen Sie JSON in Java
Interpreter-Implementierung durch Java
Machen Sie einen Blackjack mit Java
Spielen Sie Framework2.5 (Java) -Tipps
Janken App in Java
Einschränkungsprogrammierung in Java
Setzen Sie Java8 in Centos7
NVL-artiger Typ in Java
Verbinden Sie Arrays in Java
Aufrufbare Schnittstelle in Java
Kommentare in der Java-Quelle
Azure funktioniert in Java
Formatieren Sie XML in Java
Einfache HTML-Spezialchars in Java
Boyer-Moore-Implementierung in Java
Hallo Welt in Java
Verwenden Sie OpenCV mit Java
WebApi-Memorandum mit Java
Befehle in Java ausführen (Ping)
Verschiedene Threads in Java
Implementierung der Heap-Sortierung (in Java)
Zabbix API in Java
ASCII-Kunst in Java
Listen in Java vergleichen
POST JSON in Java
Fehler in Java ausdrücken
Erstellen Sie JSON in Java
Datumsmanipulation in Java 8
Was ist neu in Java 8?
Verwenden Sie PreparedStatement in Java
Was ist neu in Java 9,10,11
Parallele Ausführung in Java
Spielen Sie andere Codewerte als XML-gültige Zeichen in Java ab
Lesen Sie WAV-Daten als Byte-Array unter Android Java
So spielen Sie eine MIDI-Datei mit der Java Sound API ab