diff options
Diffstat (limited to 'src/sound')
-rw-r--r-- | src/sound/AudioFilePlayer.java | 100 | ||||
-rw-r--r-- | src/sound/ShortAudioPlayer.java | 170 |
2 files changed, 84 insertions, 186 deletions
diff --git a/src/sound/AudioFilePlayer.java b/src/sound/AudioFilePlayer.java index 9fc1afc..9425881 100644 --- a/src/sound/AudioFilePlayer.java +++ b/src/sound/AudioFilePlayer.java @@ -1,22 +1,15 @@ -/*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; +//Java program to play audio files. imports file scanning and various +//methods from the java audio class in order to do so. public class AudioFilePlayer { @@ -38,101 +31,36 @@ public class AudioFilePlayer audioFile = new File(filePath); // create AudioInputStream object try { - audioInputStream = - AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); - } catch (UnsupportedAudioFileException e) { - // TODO Auto-generated catch block + audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); + } + catch (UnsupportedAudioFileException e) { e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block + } + catch (IOException e) { e.printStackTrace(); } // create clip reference try { clip = AudioSystem.getClip(); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block + } + catch (LineUnavailableException e) { e.printStackTrace(); } // open audioInputStream to the clip try { clip.open(audioInputStream); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block + } + catch (LineUnavailableException e) { e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block + } + catch (IOException e) { e.printStackTrace(); } } - - // public static void main(String[] args) - // { - // try - // { - // filePath = "src/assets/BookBetrayal.wav3"; - // 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 - - public 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() 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 + } +} |