我正在使用以下代码在设备接近任何对象时将音频流切换到耳机:
@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);
(&gt; 500毫秒)
使用Android的默认MediaPlayer,可以毫不拖延地更改输出流:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); // ear piece
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // speakerphone
有没有办法改变ExoPlayer的输出流?
答案 0 :(得分:2)
要更改 ExoPlayer的流类型,您需要通过MediaCodecAudioTrackRenderer构造函数将流类型传递到(ExoPlayer&#39; s)AudioTrack构造函数,
public AudioTrack() {
this(null, AudioManager.STREAM_MUSIC); //default is STREAM_MUSIC
}
public AudioTrack(AudioCapabilities audioCapabilities, int streamType) {
}
因此,在您的应用程序中,您将在构建渲染器时指定类型。
答案 1 :(得分:0)
通过耳机播放
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);