Пример #1
0
  public JavaSound(File file) {
    this.file = file;

    try {
      clip = AudioSystem.getClip();
    } catch (LineUnavailableException e) {
      ForPlay.log().warn("Unable to create clip for " + file);
      // give up
      return;
    } catch (IllegalArgumentException e) {
      /*
       * OpenJDK on Linux may throw java.lang.IllegalArgumentException: No line matching interface
       * Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame,
       * big-endian is supported.
       */
      ForPlay.log().info("Failed to load sound " + file + " due to " + e.toString());
      // give up
      return;
    }

    clip.addLineListener(
        new LineListener() {
          @Override
          public void update(LineEvent event) {
            Type type = event.getType();
            if (LineEvent.Type.STOP == type) {
              // ForPlay.log().info("STOP EVENT");
              try {
                clip.close();
              } finally {
                playing = false;
              }
            }
          }
        });
  }
Пример #2
0
  @Override
  public boolean play() {
    // ForPlay.log().info("play()");
    if (playing) {
      // we have not yet received LineEvent.Type.STOP
      return false;
    }
    if (clip == null) {
      // no audio clip to play
      return false;
    }
    if (clip.isActive()) {
      // already playing
      return false;
    }

    try {
      inputStream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
      ForPlay.log().warn("Sound file not found " + file);
      // give up
      return false;
    }

    AudioInputStream ais;
    try {
      // ForPlay.log().info("calling AudioSystem.getAudioInputStream()");
      ais = AudioSystem.getAudioInputStream(inputStream);
    } catch (UnsupportedAudioFileException e) {
      ForPlay.log()
          .warn(
              "Failed to play sound "
                  + file
                  + " due to failure to get audio stream caused by "
                  + e.toString(),
              e);
      return false;
    } catch (IOException e) {
      ForPlay.log()
          .warn(
              "Failed to play sound "
                  + file
                  + " due to failure to get audio stream caused by "
                  + e.toString(),
              e);
      return false;
    }
    try {
      // ForPlay.log().info("calling clip.open()");
      clip.open(ais);
    } catch (IOException e) {
      ForPlay.log()
          .warn(
              "Failed to play sound "
                  + file
                  + " due to failure to open clip caused by "
                  + e.toString(),
              e);
      return false;
    } catch (LineUnavailableException e) {
      ForPlay.log()
          .info(
              "Not playing sound "
                  + file
                  + " due to failure to open clip caused by "
                  + e.toString());
    } catch (IllegalArgumentException e) {
      ForPlay.log()
          .info(
              "Not playing sound "
                  + file
                  + " due to failure to open clip caused by "
                  + e.toString());
      return false;
    } catch (IllegalStateException e) {
      // Line may already be open
      // TODO(fredsa): figure out why this happens
      ForPlay.log()
          .info(
              "Not playing sound "
                  + file
                  + " due to failure to open clip caused by "
                  + e.toString());
      return false;
    }
    if (looping) {
      clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    // ForPlay.log().info("calling clip.start()");
    clip.start();
    playing = true;
    return true;
  }