Ejemplo n.º 1
0
  private void doPlay(File file) {
    AudioInputStream actualAudioIn = null;
    this.file = file;
    try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(file)) {
      AudioFormat baseFormat = audioIn.getFormat();
      System.out.println(baseFormat.getEncoding());
      AudioFormat decodedFormat = getDecodedFormat(baseFormat);

      // make new audio in stream based on decoded format
      actualAudioIn = AudioSystem.getAudioInputStream(decodedFormat, audioIn);

      // get data from audio system
      line = getLine(decodedFormat);

      line.addLineListener(this);

      doPlay(decodedFormat, actualAudioIn);
      audioIn.close();
    } catch (Exception e) {
      logger.log(Level.WARNING, "Exception playing file '" + file.getName() + "'", e);
    } finally {
      if (actualAudioIn != null) {
        try {
          actualAudioIn.close();
        } catch (IOException e) {
        }
      }
      if (line != null) line.close();
    }
  }
Ejemplo n.º 2
0
 public void addLineListener(LineListener listener) {
   dataLine.addLineListener(listener);
 }
Ejemplo n.º 3
0
  /**
   * Single audio channel playback with automatic starting and stopping of the underlying
   * sourcedataline specified by the mixer and mixer channel arguments.
   *
   * <p>Maintains an internal non-blocking audio packet queue and processes this queue 25 times a
   * second (every 40 ms).
   *
   * @param threadPoolManager for assigning buffer processing schedule task
   * @param mixer to obtain source data line
   * @param mixerChannel either mono or left/right stereo
   * @param audioFormat to use during playback
   * @param lineInfo to use when obtaining the source data line
   * @param requestedBufferSize of approximately 1 second of audio
   */
  public AudioOutput(
      ThreadPoolManager threadPoolManager,
      Mixer mixer,
      MixerChannel mixerChannel,
      AudioFormat audioFormat,
      Line.Info lineInfo,
      int requestedBufferSize) {
    mThreadPoolManager = threadPoolManager;
    mMixer = mixer;
    mMixerChannel = mixerChannel;

    try {
      mOutput = (SourceDataLine) mMixer.getLine(lineInfo);

      if (mOutput != null) {
        mOutput.open(audioFormat, requestedBufferSize);

        // Start threshold: buffer is full with 10% or less of capacity remaining
        mBufferStartThreshold = (int) (mOutput.getBufferSize() * 0.10);

        // Stop threshold: buffer is empty with 90% or more capacity available
        mBufferStopThreshold = (int) (mOutput.getBufferSize() * 0.90);

        mOutput.addLineListener(this);

        if (mOutput != null) {
          try {
            Control gain = mOutput.getControl(FloatControl.Type.MASTER_GAIN);
            mGainControl = (FloatControl) gain;
          } catch (IllegalArgumentException iae) {
            mLog.warn(
                "Couldn't obtain MASTER GAIN control for stereo line ["
                    + mixer.getMixerInfo().getName()
                    + " | "
                    + getChannelName()
                    + "]");
          }

          try {
            Control mute = mOutput.getControl(BooleanControl.Type.MUTE);
            mMuteControl = (BooleanControl) mute;
          } catch (IllegalArgumentException iae) {
            mLog.warn(
                "Couldn't obtain MUTE control for stereo line ["
                    + mixer.getMixerInfo().getName()
                    + " | "
                    + getChannelName()
                    + "]");
          }

          /* Run the queue processor task every 40 milliseconds or 25 times a second */
          mProcessorTask =
              mThreadPoolManager.scheduleFixedRate(
                  ThreadType.AUDIO_PROCESSING, new BufferProcessor(), 40, TimeUnit.MILLISECONDS);
        }

        mAudioStartEvent = new AudioEvent(AudioEvent.Type.AUDIO_STARTED, getChannelName());
        mAudioStopEvent = new AudioEvent(AudioEvent.Type.AUDIO_STOPPED, getChannelName());

        mCanProcessAudio = true;
      }
    } catch (LineUnavailableException e) {
      mLog.error(
          "Couldn't obtain audio source data line for "
              + "audio output - mixer ["
              + mMixer.getMixerInfo().getName()
              + "]");
    }
  }