我正在使用以下代码在设备接近任何对象时将音频流切换到耳机:
@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)时会有很大的延迟; (> 500ms)
使用Android的默认MediaPlayer,可以毫不拖延地更改输出流:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); // ear piece mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // speakerphone
有没有办法改变ExoPlayer的输出流?