aboutsummaryrefslogtreecommitdiff
path: root/src/main/AudioFilePlayer.java
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2023-05-06 14:56:46 -0400
committersowgro <tpoke.ferrari@gmail.com>2023-05-06 14:56:46 -0400
commit372d97fee538f86c8333fbbde43cf51484b8ac67 (patch)
tree055f5560e58d4a83a90a3e1c46ec8d998e12a96f /src/main/AudioFilePlayer.java
parentcee3ca3fec021c0ddf8f3ee66940bb1cdfba4262 (diff)
downloadNPEhero-372d97fee538f86c8333fbbde43cf51484b8ac67.tar.gz
NPEhero-372d97fee538f86c8333fbbde43cf51484b8ac67.tar.bz2
NPEhero-372d97fee538f86c8333fbbde43cf51484b8ac67.zip
Add JavaFX and seperate source and class files
Diffstat (limited to 'src/main/AudioFilePlayer.java')
-rw-r--r--src/main/AudioFilePlayer.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/AudioFilePlayer.java b/src/main/AudioFilePlayer.java
new file mode 100644
index 0000000..c3b9728
--- /dev/null
+++ b/src/main/AudioFilePlayer.java
@@ -0,0 +1,96 @@
+package main;
+//Java program to play audio files. imports file scanning and various
+//methods from the java audio class in order to do so.
+import java.io.File;
+import java.io.IOException;
+
+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 AudioFilePlayer implements LineListener
+{
+ //indicates whether the playback completes or not
+ boolean playCompleted;
+ Clip audioClip;
+
+ public void play(String audioFilePath)
+ {
+ File audioFile = new File(audioFilePath);
+
+ try
+ {
+ //creates an audioInput object using the file we
+ //declared earlier
+ AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
+
+ //gets the format of the audioStream object
+ AudioFormat format = audioStream.getFormat();
+
+ DataLine.Info info = new DataLine.Info(Clip.class, format);
+
+ audioClip = (Clip) AudioSystem.getLine(info);
+
+ audioClip.addLineListener(this);
+
+ audioClip.open(audioStream);
+
+ audioClip.start();
+
+ while (!playCompleted)
+ {
+ // wait for the playback to complete
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ audioClip.close(); //stops the audio clip
+ }
+ catch (UnsupportedAudioFileException ex)
+ {
+ System.out.println("The specified audio file is not supported.");
+ ex.printStackTrace();
+ }
+ catch (LineUnavailableException ex)
+ {
+ System.out.println("Audio line for playing back is unavailable.");
+ ex.printStackTrace();
+ }
+ catch (IOException ex)
+ {
+ System.out.println("Error playing the audio file.");
+ ex.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Listens to the START and STOP events of the audio line.
+ */
+ @Override
+ public void update(LineEvent event)
+ {
+ LineEvent.Type type = event.getType();
+
+ if (type == LineEvent.Type.START)
+ {
+ System.out.println("Playback started.");
+ }
+ else if (type == LineEvent.Type.STOP)
+ {
+ playCompleted = true;
+ System.out.println("Playback completed.");
+ }
+ }
+}