Esempio n. 1
0
 // Play or Re-play the sound effect from the beginning, by rewinding.
 public void play() {
   if (volume != Volume.MUTE) {
     if (clip.isRunning()) clip.stop(); // Stop the player if it is still running
     clip.setFramePosition(0); // rewind to the beginning
     clip.start(); // Start playing
   }
 }
Esempio n. 2
0
 public static void loopClip(Clip clip, int n) {
   if (clip != null) {
     clip.stop();
     clip.setFramePosition(0);
     clip.loop(n);
   }
 }
Esempio n. 3
0
 public static void playClip(Clip clip) {
   if (clip != null) {
     clip.stop();
     clip.setFramePosition(0);
     clip.start();
   }
 }
Esempio n. 4
0
 /*routine to play exploding sound*/
 public void explode() {
   File sound = new File("explosion.wav");
   try {
     Clip clip = AudioSystem.getClip();
     clip.open(AudioSystem.getAudioInputStream(sound)); // open the specified clip
     clip.start(); // play it
   } catch (Exception e) {
     System.out.println(e);
   }
 }
Esempio n. 5
0
  public static Clip loadClip(URL url) {
    Clip clip = null;
    String fnm = "" + url;
    try {
      AudioInputStream stream = AudioSystem.getAudioInputStream(url);
      AudioFormat format = stream.getFormat();

      if ((format.getEncoding() == AudioFormat.Encoding.ULAW)
          || (format.getEncoding() == AudioFormat.Encoding.ALAW)) {
        AudioFormat newFormat =
            new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                format.getSampleRate(),
                format.getSampleSizeInBits() * 2,
                format.getChannels(),
                format.getFrameSize() * 2,
                format.getFrameRate(),
                true); // big endian
        stream = AudioSystem.getAudioInputStream(newFormat, stream);
        // System.out.println("Converted Audio format: " + newFormat);
        format = newFormat;
      }

      DataLine.Info info = new DataLine.Info(Clip.class, format);

      // make sure sound system supports data line
      if (!AudioSystem.isLineSupported(info)) {
        System.out.println("Unsupported Clip File: " + fnm);
        return null;
      }
      // get clip line resource
      clip = (Clip) AudioSystem.getLine(info);
      clip.open(stream); // open the sound file as a clip
      stream.close(); // we're done with the input stream
      // duration (in secs) of the clip
      double duration = clip.getMicrosecondLength() / 1000000.0; // new
      if (duration <= 1.0) {
        System.out.println("WARNING. Duration <= 1 sec : " + duration + " secs");
        System.out.println(
            "         The clip in " + fnm + " may not play in J2SE 1.5 -- make it longer");
      }
      // else
      //  System.out.println(fnm + ": Duration: " + duration + " secs");
    } // end of try block
    catch (UnsupportedAudioFileException audioException) {
      System.out.println("Unsupported audio file: " + fnm);
    } catch (LineUnavailableException noLineException) {
      System.out.println("No audio line available for : " + fnm);
    } catch (IOException ioException) {
      System.out.println("Could not read: " + fnm);
    } catch (Exception e) {
      System.out.println("Problem with " + fnm);
    }
    return clip;
  } // end of loadClip()
Esempio n. 6
0
 /*Routine to play theme*/
 public void playTheme() {
   File sound = new File("Death on the Dance Floor_1.wav");
   try {
     Clip clip = AudioSystem.getClip();
     clip.open(AudioSystem.getAudioInputStream(sound)); // open the specified clip
     clip.start(); // start it
     clip.loop(Clip.LOOP_CONTINUOUSLY); // loop it
   } catch (Exception e) {
     System.out.println(e);
   }
 }
Esempio n. 7
0
 private void loadAndPlayAudio(String audioResource) {
   try (AudioInputStream soundStream =
       AudioSystem.getAudioInputStream(getClass().getResource(audioResource))) {
     DataLine.Info info = new DataLine.Info(Clip.class, soundStream.getFormat());
     Clip clip = (Clip) AudioSystem.getLine(info);
     clip.open(soundStream);
     clip.start();
   } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
     ex.printStackTrace();
   }
 }
Esempio n. 8
0
  public void playAudio() {

    if (audio != null) {
      audio.stop();
      audio.setFramePosition(0);
      audio.start();
      if (loops.get(0) == true) {
        audio.loop(Clip.LOOP_CONTINUOUSLY);
      }
    }
  }
Esempio n. 9
0
  public void playStop() {

    if (clip != null) {
      if (playState == PLAYING || playState == PAUSED) {

        clip.stop();
        clip.close();
        playState = STOPPED;
      }
    }
  }
Esempio n. 10
0
  public void playLoop() {

    if (clip != null) {

      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      clip.setMicrosecondPosition((long) playStartTime);
      if (playState != PLAYING) {
        clip.start();
        playState = PLAYING;
      }
    }
  }
Esempio n. 11
0
 public static void main(String[] args) {
   try {
     byte[] wav = Base64Coder.base64ToBinary(getWaveLiteral().toCharArray(), 0, WAV_SIZE);
     InputStream is = new ByteArrayInputStream(wav);
     AudioFormat fmt = AudioSystem.getAudioFileFormat(is).getFormat();
     AudioInputStream sound = AudioSystem.getAudioInputStream(is);
     DataLine.Info info = new DataLine.Info(Clip.class, fmt);
     Clip clip = (Clip) AudioSystem.getLine(info);
     clip.open(sound);
     clip.start();
     Thread.sleep(3000);
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Esempio n. 12
0
  public double playGetTime() {

    if (clip != null) {
      return ((double) clip.getMicrosecondPosition()) / 1000.0;
    }
    return 0;
  }
Esempio n. 13
0
 private void soundAbspielen(File sound) {
   if (!läuft) return;
   try {
     audioInputStream = AudioSystem.getAudioInputStream(sound);
     af = audioInputStream.getFormat();
     size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
     audio = new byte[size];
     info = new DataLine.Info(Clip.class, af, size);
     audioInputStream.read(audio, 0, size);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(af, audio, 0, size);
     clip.start();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Esempio n. 14
0
  private void playAudio(int store)
        //  PRE:  store must correspond to one of the existing stores.
        //  POST: Plays the audio associated with store. All files should be located in
        //        the \audio\ file path.
      {
    AudioInputStream audio; // The audio stream.
    String filePath; // The file path to the audio file.
    File audioFile; // The audio file.
    Clip audioClip; // The audio clip.

    switch (store) // create file path for audio associated with store
    {
      case -1:
        filePath = ".\\audio\\click.wav";
        break;

      case 0:
        filePath = ".\\audio\\weaponSmith.wav";
        break;

      case 1:
        filePath = ".\\audio\\armorSmith.wav";
        break;

      case 2:
        filePath = ".\\audio\\accessoryMerchant.wav";
        break;

      default:
        filePath = ".\\audio\\generalMerchant.wav";
    }

    audioFile = null;

    try {
      audioClip = AudioSystem.getClip();
      audioFile = new File(filePath);
      audio = AudioSystem.getAudioInputStream(audioFile);
      audioClip.open(audio);
      audioClip.start();
    } catch (Exception e) // if there was an error playing the clip
    {
      System.err.println("Error playing clip!");
    }
  }
Esempio n. 15
0
 public static void Play() {
   try {
     // Open an audio input stream.
     File soundFile = new File("foo.wav");
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
     // Get a sound clip resource.
     Clip clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioIn);
     clip.start();
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Esempio n. 16
0
  public void playResume() {

    if (clip != null) {
      if (playState == PAUSED) {

        clip.start();
        playState = PLAYING;
      }
    }
  }
Esempio n. 17
0
  public void playPause() {

    if (clip != null) {
      if (playState == PLAYING) {

        clip.stop();
        playState = PAUSED;
      }
    }
  }
Esempio n. 18
0
 private static void playWav(String name, boolean loop, double volume)
     throws FileNotFoundException, IOException, UnsupportedAudioFileException,
         LineUnavailableException {
   AudioInputStream ais = AudioSystem.getAudioInputStream(new File(path + name));
   Clip clip = AudioSystem.getClip();
   clip.open(ais);
   if (loop) {
     clip.loop(-1);
   }
   ((FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN))
       .setValue((float) (Math.log(volume) / Math.log(10.) * 20.));
   clip.start();
   wavMap.put(name, clip);
   //        // open the sound file as a Java input stream
   //        InputStream in = new FileInputStream(path + name);
   //        // create an audiostream from the inputstream
   //        AudioStream audioStream = new AudioStream(in);
   //        // play the audio clip with the audioplayer class
   //        AudioPlayer.player.start(audioStream);
   //        wavMap.put(name, audioStream);
 }
Esempio n. 19
0
 public void addSound(String fileName, boolean loop) {
   try {
     input = AudioSystem.getAudioInputStream(new File(fileName));
     audio = AudioSystem.getClip();
     audio.open(input);
     clips.add(audio);
     loops.add(loop);
   } catch (IOException e) {
     System.out.println("Audio File Not Found");
   } catch (UnsupportedAudioFileException e) {
     System.out.println("Audio File Type Not Supported, brah");
   } catch (LineUnavailableException e) {
     // TODO Auto-generated catch block
     System.out.println("Serious audio problem Line Unavailable");
   }
 }
Esempio n. 20
0
 // Constructor to construct each element of the enum with its own sound file.
 SoundEffect(String soundFileName) {
   try {
     // Use URL (instead of File) to read from disk and JAR.
     URL url = this.getClass().getClassLoader().getResource(soundFileName);
     // Set up an audio input stream piped from the sound file.
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
     // Get a clip resource.
     clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioInputStream);
   } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (LineUnavailableException e) {
     e.printStackTrace();
   }
 }
Esempio n. 21
0
  public boolean play() {

    try {
      if (playState != STOPPED) playStop();

      if (audioBytes == null) return false;

      DataLine.Info info = new DataLine.Info(Clip.class, format);

      clip = (Clip) AudioSystem.getLine(info);
      clip.addLineListener(new ClipListener());

      long clipStart = (long) (audioBytes.length * getStartTime() / (getDuration() * 1000.0));
      long clipEnd = (long) (audioBytes.length * getEndTime() / (getDuration() * 1000.0));
      if ((clipEnd - clipStart) > MAX_CLIP_LENGTH) clipEnd = clipStart + MAX_CLIP_LENGTH;
      byte[] clipBytes = new byte[(int) (clipEnd - clipStart)];
      System.arraycopy(audioBytes, (int) clipStart, clipBytes, 0, clipBytes.length);
      clip.open(format, clipBytes, 0, clipBytes.length);

      FloatControl panControl = (FloatControl) clip.getControl(FloatControl.Type.PAN);

      panControl.setValue((float) panSetting / 100.0f);

      double value = (double) gainSetting;

      FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
      float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
      gainControl.setValue(dB);
      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      clip.setMicrosecondPosition((long) playStartTime);

      clip.start();

      playState = PLAYING;

      return true;

    } catch (Exception ex) {
      ex.printStackTrace();
      playState = STOPPED;
      clip = null;
      return false;
    }
  }
Esempio n. 22
0
 public static int clipLength(Clip clip) {
   if (clip != null) {
     return (int) (clip.getMicrosecondLength() / 1000);
   }
   return 0;
 }
Esempio n. 23
0
 public void stopAudio() {
   pauseAudio();
   audio.setFramePosition(0);
 }
Esempio n. 24
0
 public void pauseAudio() {
   audio.stop();
 }
Esempio n. 25
0
 public void setVolume(float volume) {
   gain = (FloatControl) audio.getControl(FloatControl.Type.MASTER_GAIN);
   gain.setValue(volume);
 }
Esempio n. 26
0
 public double playGetLength() {
   if (clip != null) {
     return clip.getMicrosecondLength() * 1000000.0;
   }
   return 0.0;
 }
Esempio n. 27
0
 public static void stopClip(Clip clip) {
   if (clip != null) {
     clip.stop();
     // clip.close();
   }
 }
Esempio n. 28
0
  public void playSetSeek(double seekTime) {

    if (clip != null) {
      clip.setMicrosecondPosition((long) (seekTime * 1000.0));
    }
  }