diff --git a/javax-sound/pom.xml b/javax-sound/pom.xml new file mode 100644 index 0000000000..7b5fabb9a7 --- /dev/null +++ b/javax-sound/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + com.baeldung.javax-sound + javax-sound + 1.0-SNAPSHOT + javax-sound + jar + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + javazoom + jlayer + ${javazoom.jlayer.version} + + + org.openjfx + javafx-controls + ${org.openjfx.version} + + + org.openjfx + javafx-media + ${org.openjfx.version} + + + + 11.0.2 + 1.0.1 + + \ No newline at end of file diff --git a/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingClip.java b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingClip.java new file mode 100644 index 0000000000..6f7ec4013f --- /dev/null +++ b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingClip.java @@ -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); + } + +} \ No newline at end of file diff --git a/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaFx.java b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaFx.java new file mode 100644 index 0000000000..523376936d --- /dev/null +++ b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaFx.java @@ -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()); + } + + } +} diff --git a/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaZoom.java b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaZoom.java new file mode 100644 index 0000000000..a7174b29a1 --- /dev/null +++ b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingJavaZoom.java @@ -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()); + } + } +} diff --git a/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingSourceDataLine.java b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingSourceDataLine.java new file mode 100644 index 0000000000..2b589554c2 --- /dev/null +++ b/javax-sound/src/main/java/com/baeldung/SoundPlayerUsingSourceDataLine.java @@ -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); + } + +} diff --git a/javax-sound/src/main/resources/AudioFileWithMp3Format.mp3 b/javax-sound/src/main/resources/AudioFileWithMp3Format.mp3 new file mode 100644 index 0000000000..53dbb249fa Binary files /dev/null and b/javax-sound/src/main/resources/AudioFileWithMp3Format.mp3 differ diff --git a/pom.xml b/pom.xml index 7cd2bbe3d1..e9942d77e8 100644 --- a/pom.xml +++ b/pom.xml @@ -437,6 +437,7 @@ java-websocket javax-servlets javax-servlets-2 + javax-sound javaxval jaxb jee-7 @@ -898,6 +899,7 @@ java-websocket javax-servlets javax-servlets-2 + javax-sound javaxval jaxb jee-7