Audio AudiManager从蓝牙更改为耳机不起作用
我有一个按钮,应该从耳机到扬声器再到蓝牙等等 这是我的代码:Audio AudiManager从蓝牙更改为耳机不起作用,audio,bluetooth,phone-call,android-audiomanager,speaker,Audio,Bluetooth,Phone Call,Android Audiomanager,Speaker,我有一个按钮,应该从耳机到扬声器再到蓝牙等等 这是我的代码: fun setSpeakerValue(value: SIPManager.AUDIO) { speaker = value when (value) { SIPManager.AUDIO.EAR_PIECE -> { Log.i("Speaker", "Speaker1 EARPIECE") binding
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
}
}
}
但当我听耳机时,我停止蓝牙,我将isSpeakerOn设置为false,但之后,我开始用蓝牙而不是手机的听筒来听。我做错了什么?使用下面的答案让它工作:
我想到的一些选择1。move audioManager?.isBluetoothScoOn=超出if作用域2的错误语句。对于AUDIO.EAR\u片段3,是否有单独的标志/布尔值。您确定BluetoothSCO是否已断开连接(可以从日志中确认)?1.如果我将其移到外部,则没有帮助。2.我没有布尔值,它只是一个枚举,用于3个状态SIPManager.AUDIO.*3。我现在会检查,但当我按下按钮时,我确实听到耳机上有一声嘟嘟声,表示它已断开连接,几秒钟后它才再次启动。我猜audioManager的模式有一个频道,在它配置为蓝牙后,当我尝试使用耳机时,它会是相同的吗?我不确定,试过对每种类型使用不同的模式,但它可能是1无法正常工作,或者另一个1可能会在设置和断开蓝牙连接时出现延迟问题。这是否有助于在蓝牙连接和断开连接后添加延迟?我将在完成当前任务后进行测试,并会让您知道我使用anwer中的示例使其正常工作。还添加了原始问题的链接。
//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);
}