diff options
author | Zach Jordan <zxjordan5@gmail.com> | 2023-05-10 12:32:20 +0000 |
---|---|---|
committer | Zach Jordan <zxjordan5@gmail.com> | 2023-05-10 12:32:20 +0000 |
commit | 49bcaaabbc4996ff790e4253bad9deadf3ffe42b (patch) | |
tree | 642fc6a7c863bab536363bdbdf5cb7869c344ff6 /src/sound/AudioFilePlayer.java | |
parent | 49203cc510dc71f20650e9a4eb0ed717d0df7933 (diff) | |
download | NPEhero-49bcaaabbc4996ff790e4253bad9deadf3ffe42b.tar.gz NPEhero-49bcaaabbc4996ff790e4253bad9deadf3ffe42b.tar.bz2 NPEhero-49bcaaabbc4996ff790e4253bad9deadf3ffe42b.zip |
first draft of audio file players (may require tweaking to work with buttons)
Diffstat (limited to 'src/sound/AudioFilePlayer.java')
-rw-r--r-- | src/sound/AudioFilePlayer.java | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/src/sound/AudioFilePlayer.java b/src/sound/AudioFilePlayer.java new file mode 100644 index 0000000..06f728b --- /dev/null +++ b/src/sound/AudioFilePlayer.java @@ -0,0 +1,197 @@ +/*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 java.util.Scanner; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + +public class AudioFilePlayer +{ + + // to store current position + Long currentFrame; + Clip clip; + + // current status of clip + String status; + + AudioInputStream audioInputStream; + static String filePath; + + // constructor to initialize streams and clip + public AudioFilePlayer() throws UnsupportedAudioFileException, + IOException, LineUnavailableException + { + // create AudioInputStream object + audioInputStream = + AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); + + // create clip reference + clip = AudioSystem.getClip(); + + // open audioInputStream to the clip + clip.open(audioInputStream); + + clip.loop(Clip.LOOP_CONTINUOUSLY); + } + + public static void main(String[] args) + { + try + { + filePath = "GH3Intro.wav"; + AudioFilePlayer audioPlayer = new AudioFilePlayer(); + + audioPlayer.play(); + Scanner sc = new Scanner(System.in); + + while (true) //until the thread closes, ask the user what they want to do with the audio file + { + System.out.println("1. pause"); + System.out.println("2. resume"); + System.out.println("3. restart"); + System.out.println("4. stop"); + System.out.println("5. Jump to specific time"); + int c = sc.nextInt(); + audioPlayer.gotoChoice(c); + if (c == 4) + break; + } + sc.close(); + } + + catch (Exception ex) + { + System.out.println("Error with playing sound."); + ex.printStackTrace(); + + } + } + + // Work as the user enters his choice + + private void gotoChoice(int c)throws IOException, LineUnavailableException, UnsupportedAudioFileException + { + //reads the users input and chooses what to do based on said input + switch (c) + { + case 1: + pause(); + break; + case 2: + resumeAudio(); + break; + case 3: + restart(); + break; + case 4: + stop(); + break; + case 5: + System.out.println("Enter time (" + 0 + + ", " + clip.getMicrosecondLength() + ")"); + Scanner sc = new Scanner(System.in); + long c1 = sc.nextLong(); + jump(c1); + break; + + } + + } + + // Method to play the audio + public void play() + { + //start the clip + clip.start(); + + status = "play"; + } + + // Method to pause the audio + public void pause() + { + if (status.equals("paused")) + { + System.out.println("audio is already paused"); + return; + } + this.currentFrame = this.clip.getMicrosecondPosition(); + clip.stop(); + status = "paused"; + } + + // Method to resume the audio + public void resumeAudio() throws UnsupportedAudioFileException, + IOException, LineUnavailableException + { + if (status.equals("play")) + { + System.out.println("Audio is already "+ + "being played"); + return; + } + clip.close(); + resetAudioStream(); + clip.setMicrosecondPosition(currentFrame); + this.play(); + } + + // Method to restart the audio + public void restart() throws IOException, LineUnavailableException, + UnsupportedAudioFileException + { + clip.stop(); + clip.close(); + resetAudioStream(); + currentFrame = 0L; + clip.setMicrosecondPosition(0); + this.play(); + } + + // Method to stop the audio + public void stop() throws UnsupportedAudioFileException, + IOException, LineUnavailableException + { + currentFrame = 0L; + clip.stop(); + clip.close(); + } + + // Method to jump over a specific part + public void jump(long c) throws UnsupportedAudioFileException, IOException, + LineUnavailableException + { + if (c > 0 && c < clip.getMicrosecondLength()) + { + clip.stop(); + clip.close(); + resetAudioStream(); + currentFrame = c; + clip.setMicrosecondPosition(c); + this.play(); + } + } + + // Method to reset audio stream + public void resetAudioStream() throws UnsupportedAudioFileException, IOException, LineUnavailableException + { + audioInputStream = AudioSystem.getAudioInputStream( + new File(filePath).getAbsoluteFile()); + clip.open(audioInputStream); + clip.loop(Clip.LOOP_CONTINUOUSLY); + } + +}
\ No newline at end of file |