AudioTrack 在Java应用中,管理和播放一个单一的语音资源
The AudioTrack class manages and plays a single audio resource for Java applications.
* It allows streaming of PCM audio buffers to the audio sink for playback. This is
* achieved by "pushing" the data to the AudioTrack object using one of the
* {@link #write(byte[], int, int)} and {@link #write(short[], int, int)} methods.
一个AudioTrack 实例可以在两种模式下运行:static和streaming模式
在Streaming模式下,应用调用write()方法,向AudioTrack中写入一段连续的数据流。
这个操作处于阻塞状态,直到数据从java层传递到native层,并且加入播放队列。才返回
streaming模式最适用于 以下音频数据块:
(1)音乐长度太长。导致太大放不进内存
(2)音乐质量太高,导致太大放不进内存。(高取样率,采样位数)
(3)在队列中的前一个audio正在播放时,接收到或生成。
* <p>An AudioTrack instance can operate under two modes: static or streaming.<br>
* In Streaming mode, the application writes a continuous stream of data to the AudioTrack, using
* one of the {@code write()} methods. These are blocking and return when the data has been
* transferred from the Java layer to the native layer and queued for playback. The streaming
* mode is most useful when playing blocks of audio data that for instance are:
*
* <ul>
* <li>too big to fit in memory because of the duration of the sound to play,</li>
* <li>too big to fit in memory because of the characteristics of the audio data
* (high sampling rate, bits per sample ...)</li>
* <li>received or generated while previously queued audio is playing.</li>
* </ul>
*
static模式适用于 处理可以放在内存中的较短,且需要小的播放开销的声音片段
因此static模式适用于UI声音 和 游戏声音这种经常被播放的情况,开销很小。
* The static mode should be chosen when dealing with short sounds that fit in memory and
* that need to be played with the smallest latency possible. The static mode will
* therefore be preferred for UI and game sounds that are played often, and with the
* smallest overhead possible.
被创建后,一个AudioTrack对象初始化和它相关的音频缓存
缓存的大小在构造方法中被详细指定,决定了一个AudioTrack在用完数据之前能播放多长
对于一个使用static模式的AudioTrack,定义的size大小,是它能播放的声音片段大小的最大值
对使用streaming模式的, 写入audio sink(音频信宿)中的数据总和小于等于缓存大小。
AudioTrack 不是 final的,所以可以有子类,但是不推荐这么做
* <p>Upon creation, an AudioTrack object initializes its associated audio buffer.
* The size of this buffer, specified during the construction, determines how long an AudioTrack
* can play before running out of data.<br>
* For an AudioTrack using the static mode, this size is the maximum size of the sound that can
* be played from it.<br>
* For the streaming mode, data will be written to the audio sink in chunks of
* sizes less than or equal to the total buffer size.
*
* AudioTrack is not final and thus permits subclasses, but such use is not recommended.