aboutsummaryrefslogtreecommitdiff
path: root/src/sound/ShortAudioPlayer.java
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2023-06-02 00:48:13 -0400
committersowgro <tpoke.ferrari@gmail.com>2023-06-02 00:48:13 -0400
commit4e43d6f020d908ccd9b8a6b77803cac943da00ed (patch)
treeae0eb103049a5838ebc7f1de35cc16988a4f07d8 /src/sound/ShortAudioPlayer.java
parent48bfed142f7175809a43037fb695b6fcdc4146b1 (diff)
downloadNPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.tar.gz
NPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.tar.bz2
NPEhero-4e43d6f020d908ccd9b8a6b77803cac943da00ed.zip
Finish levelUtility and project cleanup
Diffstat (limited to '')
-rw-r--r--src/sound/ShortAudioPlayer.java170
1 files changed, 70 insertions, 100 deletions
diff --git a/src/sound/ShortAudioPlayer.java b/src/sound/ShortAudioPlayer.java
index e81c4ee..77fb52c 100644
--- a/src/sound/ShortAudioPlayer.java
+++ b/src/sound/ShortAudioPlayer.java
@@ -1,103 +1,73 @@
-/*Name:
- *Date:
- *Period:
- *Teacher:
- *Description:
- */
package sound;
- //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;
+import java.io.File;
+import java.io.IOException;
- public class ShortAudioPlayer 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.");
- // }
- }
- }
+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;
+
+//Java program to play audio files. imports file scanning and various
+//methods from the java audio class in order to do so.
+public class ShortAudioPlayer 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();
+ }
+ 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)
+ {
+ //something should prolly go here
+ }
+}