类 AudioPlayer

java.lang.Object
io.agentscope.core.model.tts.AudioPlayer

public class AudioPlayer extends Object
Audio player for playing PCM audio data in real-time.

This player uses Java Sound API (javax.sound.sampled) to play audio data. It supports both synchronous and asynchronous playback.

Example usage:


 AudioPlayer player = AudioPlayer.builder()
     .sampleRate(24000)
     .build();

 player.start();

 // Play audio chunks as they arrive
 ttsModel.push("hello").subscribe(player::play);

 player.stop();
 
  • 嵌套类概要

    嵌套类
    修饰符和类型
    说明
    static class 
    Builder for constructing AudioPlayer instances.
  • 方法概要

    修饰符和类型
    方法
    说明
    Creates a new builder for AudioPlayer.
    void
    Waits for all queued audio to finish playing.
    void
    Interrupts current playback and clears the queue, but keeps the audio line open.
    void
    play(byte[] audioData)
    Queues audio data for asynchronous playback.
    void
    play(AudioBlock audioBlock)
    Plays an AudioBlock.
    void
    playSync(byte[] audioData)
    Plays audio data synchronously.
    void
    Starts the audio player and opens the audio line.
    void
    Stops the audio player and closes the audio line.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 方法详细资料

    • start

      public void start()
      Starts the audio player and opens the audio line.
      抛出:
      TTSException - if audio line cannot be opened
    • playSync

      public void playSync(byte[] audioData)
      Plays audio data synchronously.
      参数:
      audioData - PCM audio data
    • play

      public void play(byte[] audioData)
      Queues audio data for asynchronous playback.
      参数:
      audioData - PCM audio data
    • play

      public void play(AudioBlock audioBlock)
      Plays an AudioBlock.
      参数:
      audioBlock - the audio block to play
    • interrupt

      public void interrupt()
      Interrupts current playback and clears the queue, but keeps the audio line open.

      This is useful when you want to immediately stop current playback and start playing new audio without closing and reopening the audio line. Unlike stop(), this method keeps the audio line open so new audio can be played immediately.

    • stop

      public void stop()
      Stops the audio player and closes the audio line.
    • drain

      public void drain()
      Waits for all queued audio to finish playing.
    • builder

      public static AudioPlayer.Builder builder()
      Creates a new builder for AudioPlayer.
      返回:
      a new Builder instance