我有一个按钮,应该从EARPIECE到SPEAKER到BLUETOOTH,依此类推。
这是我的代码:
fun setSpeakerValue(value: SIPManager.AUDIO) {
speaker = value
when (value) {
SIPManager.AUDIO.EAR_PIECE -> {
Log.i("Speaker", "Speaker1 EARPIECE")
binding.callItemIconSpeaker.setImageResource(R.drawable.speaker_off)
if (SIPManager.isBluetoothConnected()) {
audioManager?.isBluetoothScoOn = false
audioManager?.stopBluetoothSco()
}
audioManager?.mode = AudioManager.MODE_NORMAL
audioManager?.isSpeakerphoneOn = false
}
SIPManager.AUDIO.SPEAKER -> {
Log.i("Speaker", "Speaker1 SPEAKER")
binding.callItemIconSpeaker.setImageResource(R.drawable.speaker_on)
if (SIPManager.isBluetoothConnected()) {
audioManager?.isBluetoothScoOn = false
audioManager?.stopBluetoothSco()
}
audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager?.isSpeakerphoneOn = true
}
SIPManager.AUDIO.BLUETOOTH -> {
Log.i("Speaker", "Speaker1 BLUETOOTH")
binding.callItemIconSpeaker.setImageResource(android.R.drawable.stat_sys_data_bluetooth)
audioManager?.mode = AudioManager.MODE_NORMAL
audioManager?.isSpeakerphoneOn = false
audioManager?.startBluetoothSco()
audioManager?.isBluetoothScoOn = true
}
}
}
但是,即使我转向听筒,我也会停止BluetoothSCO,将isSpeakerOn设置为false,但是之后,我开始在蓝牙中听而不是在手机的听筒中听。我在做什么错?
使用此处的答案使它起作用:How to switch audio output from Phone, Phone Speaker, earphones or Bluetooth device
//For BT
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
} else if(true) {
//For phone ear piece
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(false);
} else {
//For phone speaker(loudspeaker)
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(true);
}