ExoPlayer内置了一个AudioTrack音轨,可以通过以下方式启用它:
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
DefaultAudioSink audioSink = new DefaultAudioSink(audioTrack, new AudioProcessor[0]);
SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).setAudioSink(audioSink).build();
ExoPlayer播放音频时,可能会存在造成延迟的缓冲,通过调整设备输出的缓冲区大小,可以减小延迟。可以使用以下示例代码:
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
track.setPlaybackRate(sampleRate);
track.setBufferSizeInFrames(bufferSize);
通过调整播放器的缓冲大小,可以减小延迟。在创建ExoPlayer时,设置LoadControl对象即可进行缓冲区的大小调整:
LoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs).build();
SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).setLoadControl(loadControl).build();
以上是几种解决ExoPlayer播放音频延迟问题的方式,选择合适的方式便可解决问题。