我目前使用以下代码,在设备靠近任何物体时切换音频流到耳机:
@Override
public void onSensorChanged(SensorEvent event) {
if (mAudioManager.isWiredHeadsetOn() || !(mCurrentPlaybackStatus == STATUS_PLAYING
|| mCurrentPlaybackStatus == STATUS_PREPARING)) {
return;
}
boolean isClose = event.values[0] < mSensor.getMaximumRange();
if (!mScreenDisabled && isClose) {
mAudioManager.setMode(AudioManager.STREAM_MUSIC);
mAudioManager.setSpeakerphoneOn(false);
disableScreen();
mScreenDisabled = true;
} else if (mScreenDisabled && !isClose) {
mAudioManager.setSpeakerphoneOn(true);
mAudioManager.setMode(mAudioManagerMode);
enableScreen();
mScreenDisabled = false;
}
}
很遗憾,在调用.setMode(AudioManager.STREAM_MUSIC);
时会出现显著的延迟(> 500毫秒)。
使用Android的默认MediaPlayer,可以在没有延迟的情况下更改输出流:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); // ear piece
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // speakerphone
有没有办法更改ExoPlayer的输出流?