Exemplo n.º 1
0
 public static void playClip(Clip clip) {
   if (clip != null) {
     clip.stop();
     clip.setFramePosition(0);
     clip.start();
   }
 }
Exemplo n.º 2
0
  public void playResume() {

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

        clip.start();
        playState = PLAYING;
      }
    }
  }
Exemplo n.º 3
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();
   }
 }
Exemplo n.º 4
0
  public void playLoop() {

    if (clip != null) {

      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      clip.setMicrosecondPosition((long) playStartTime);
      if (playState != PLAYING) {
        clip.start();
        playState = PLAYING;
      }
    }
  }
Exemplo n.º 5
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();
   }
 }
Exemplo n.º 6
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!");
    }
  }
Exemplo n.º 7
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;
    }
  }
Exemplo n.º 8
0
  private void playNext(File file)
      throws IOException, UnsupportedAudioFileException, LineUnavailableException {
    synchronized (notificationMutex) {
      playing = true;
    }

    CancelAction cancelAction = new CancelAction();
    Application.getInstance()
        .getContext()
        .getNotificationManager()
        .showNotification(
            MessageFormat.format(
                Application.getInstance().getContext().getBundle().getString("play-voice-started"),
                file.getName()),
            cancelAction);

    try (AudioInputStream inputStream = AudioSystem.getAudioInputStream(file)) {
      clip = AudioSystem.getClip();
      clip.addLineListener(clipListener);
      clip.open(inputStream);
      clip.start();
    }
  }