BAEL-4292:Play sound in java (#12004)
* BAEL-4292:Play sound in java * BAEL-4292:Play sound in java * BAEL-4292: Play sound in java * BAEL-4292:Play sound in java * BAEL-4292:Play sound in java * BAEL-4292:Play sound in java * fix for spaces Co-authored-by: Sachin kumar <sachin.n.kumar@oracle.com>
This commit is contained in:
parent
a1b83f76f2
commit
62ac961191
|
@ -0,0 +1,37 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.javax-sound</groupId>
|
||||||
|
<artifactId>javax-sound</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>javax-sound</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javazoom</groupId>
|
||||||
|
<artifactId>jlayer</artifactId>
|
||||||
|
<version>${javazoom.jlayer.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-controls</artifactId>
|
||||||
|
<version>${org.openjfx.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-media</artifactId>
|
||||||
|
<version>${org.openjfx.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<properties>
|
||||||
|
<org.openjfx.version>11.0.2</org.openjfx.version>
|
||||||
|
<javazoom.jlayer.version>1.0.1</javazoom.jlayer.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
public class SoundPlayerUsingClip implements LineListener {
|
||||||
|
|
||||||
|
boolean isPlaybackCompleted;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(LineEvent event) {
|
||||||
|
|
||||||
|
if (LineEvent.Type.START == event.getType()) {
|
||||||
|
System.out.println("Playback started.");
|
||||||
|
} else if (LineEvent.Type.STOP == event.getType()) {
|
||||||
|
isPlaybackCompleted = true;
|
||||||
|
System.out.println("Playback completed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Play a given audio file.
|
||||||
|
* @param audioFilePath Path of the audio file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void play(String audioFilePath) {
|
||||||
|
try {
|
||||||
|
InputStream inputStream = getClass().getClassLoader()
|
||||||
|
.getResourceAsStream(audioFilePath);
|
||||||
|
AudioInputStream audioStream = AudioSystem.getAudioInputStream(inputStream);
|
||||||
|
|
||||||
|
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 (!isPlaybackCompleted) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
audioClip.close();
|
||||||
|
audioStream.close();
|
||||||
|
|
||||||
|
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException ex) {
|
||||||
|
System.out.println("Error occured during playback process:"+ ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String audioFilePath = "AudioFileWithWavFormat.wav";
|
||||||
|
|
||||||
|
// Clip can not play mpeg/mp3 format audio. We'll get exception if we run with below commented mp3 and mpeg format audio.
|
||||||
|
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
|
||||||
|
// String audioFilePath = "AudioFileWithMp3Format.mp3";
|
||||||
|
|
||||||
|
SoundPlayerUsingClip player = new SoundPlayerUsingClip();
|
||||||
|
player.play(audioFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import javafx.scene.media.Media;
|
||||||
|
import javafx.scene.media.MediaPlayer;
|
||||||
|
|
||||||
|
public class SoundPlayerUsingJavaFx {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// String audioFilePath = "AudioFileWithWavFormat.wav";
|
||||||
|
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
|
||||||
|
String audioFilePath = "AudioFileWithMp3Format.mp3";
|
||||||
|
SoundPlayerUsingJavaFx soundPlayerWithJavaFx = new SoundPlayerUsingJavaFx();
|
||||||
|
|
||||||
|
try {
|
||||||
|
com.sun.javafx.application.PlatformImpl.startup(() -> {
|
||||||
|
});
|
||||||
|
|
||||||
|
Media media = new Media(soundPlayerWithJavaFx.getClass()
|
||||||
|
.getClassLoader()
|
||||||
|
.getResource(audioFilePath)
|
||||||
|
.toExternalForm());
|
||||||
|
|
||||||
|
MediaPlayer mp3Player = new MediaPlayer(media);
|
||||||
|
mp3Player.setOnPlaying(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Playback started");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mp3Player.play();
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("Error occured during playback process:" + ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
|
||||||
|
import javazoom.jl.player.Player;
|
||||||
|
|
||||||
|
public class SoundPlayerUsingJavaZoom {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// javazoom Player doesn't work with wav audio format.
|
||||||
|
// String audioFilePath = "AudioFileWithWavFormat.wav";
|
||||||
|
|
||||||
|
// It works with below audio formats.
|
||||||
|
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
|
||||||
|
String audioFilePath = "AudioFileWithMp3Format.mp3";
|
||||||
|
SoundPlayerUsingJavaZoom player = new SoundPlayerUsingJavaZoom();
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedInputStream buffer = new BufferedInputStream(player.getClass()
|
||||||
|
.getClassLoader()
|
||||||
|
.getResourceAsStream(audioFilePath));
|
||||||
|
Player mp3Player = new Player(buffer);
|
||||||
|
mp3Player.play();
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("Error occured during playback process:" + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
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 SoundPlayerUsingSourceDataLine {
|
||||||
|
|
||||||
|
private static final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
|
void play(String soundFilePath) {
|
||||||
|
try {
|
||||||
|
InputStream inputStream = getClass().getClassLoader()
|
||||||
|
.getResourceAsStream(soundFilePath);
|
||||||
|
AudioInputStream audioStream = AudioSystem.getAudioInputStream(inputStream);
|
||||||
|
|
||||||
|
AudioFormat audioFormat = audioStream.getFormat();
|
||||||
|
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
|
||||||
|
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
|
||||||
|
sourceDataLine.open(audioFormat);
|
||||||
|
sourceDataLine.start();
|
||||||
|
|
||||||
|
System.out.println("Playback Started.");
|
||||||
|
|
||||||
|
byte[] bufferBytes = new byte[BUFFER_SIZE];
|
||||||
|
int readBytes = -1;
|
||||||
|
while ((readBytes = audioStream.read(bufferBytes)) != -1) {
|
||||||
|
sourceDataLine.write(bufferBytes, 0, readBytes);
|
||||||
|
}
|
||||||
|
sourceDataLine.drain();
|
||||||
|
sourceDataLine.close();
|
||||||
|
audioStream.close();
|
||||||
|
|
||||||
|
System.out.println("Playback has been finished.");
|
||||||
|
|
||||||
|
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException ex) {
|
||||||
|
System.out.println("Error occured during playback process:" + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String audioFilePath = "AudioFileWithWavFormat.wav";
|
||||||
|
|
||||||
|
// Clip can not play mpeg/mp3 format audio. We'll get exception if we run with below commented mp3 and mpeg format audio.
|
||||||
|
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
|
||||||
|
// String audioFilePath = "AudioFileWithMp3Format.mp3";
|
||||||
|
|
||||||
|
SoundPlayerUsingSourceDataLine player = new SoundPlayerUsingSourceDataLine();
|
||||||
|
player.play(audioFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
2
pom.xml
2
pom.xml
|
@ -437,6 +437,7 @@
|
||||||
<module>java-websocket</module>
|
<module>java-websocket</module>
|
||||||
<module>javax-servlets</module>
|
<module>javax-servlets</module>
|
||||||
<module>javax-servlets-2</module>
|
<module>javax-servlets-2</module>
|
||||||
|
<module>javax-sound</module>
|
||||||
<module>javaxval</module>
|
<module>javaxval</module>
|
||||||
<module>jaxb</module>
|
<module>jaxb</module>
|
||||||
<module>jee-7</module>
|
<module>jee-7</module>
|
||||||
|
@ -898,6 +899,7 @@
|
||||||
<module>java-websocket</module>
|
<module>java-websocket</module>
|
||||||
<module>javax-servlets</module>
|
<module>javax-servlets</module>
|
||||||
<module>javax-servlets-2</module>
|
<module>javax-servlets-2</module>
|
||||||
|
<module>javax-sound</module>
|
||||||
<module>javaxval</module>
|
<module>javaxval</module>
|
||||||
<module>jaxb</module>
|
<module>jaxb</module>
|
||||||
<module>jee-7</module>
|
<module>jee-7</module>
|
||||||
|
|
Loading…
Reference in New Issue