Audio routing fixes

This commit is contained in:
Juha Heinanen
2023-02-16 02:09:08 +02:00
parent 2a836c02ef
commit 8096a09be6
3 changed files with 24 additions and 21 deletions
@@ -1491,6 +1491,7 @@ class BaresipService: Service() {
private var btAdapter: BluetoothAdapter? = null private var btAdapter: BluetoothAdapter? = null
fun requestAudioFocus(am: AudioManager, type: Int): Boolean { fun requestAudioFocus(am: AudioManager, type: Int): Boolean {
Log.d(TAG, "Requesting audio focus of type $type")
if (audioFocusRequest != null) { if (audioFocusRequest != null) {
if (audioFocusRequest!!.audioAttributesCompat.contentType == type) if (audioFocusRequest!!.audioAttributesCompat.contentType == type)
return true return true
@@ -1515,7 +1516,7 @@ class BaresipService: Service() {
if (type == AudioAttributes.CONTENT_TYPE_SPEECH) if (type == AudioAttributes.CONTENT_TYPE_SPEECH)
am.mode = AudioManager.MODE_IN_COMMUNICATION am.mode = AudioManager.MODE_IN_COMMUNICATION
} else { } else {
Log.d(TAG, "Audio focus denied") Log.i(TAG, "Audio focus denied")
audioFocusRequest = null audioFocusRequest = null
isAudioFocused = false isAudioFocused = false
} }
@@ -197,10 +197,10 @@ class MainActivity : AppCompatActivity() {
if (device != null) { if (device != null) {
Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}") Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}")
if (speakerIcon != null) { if (speakerIcon != null) {
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
speakerIcon!!.setIcon(R.drawable.speaker_off)
else
speakerIcon!!.setIcon(R.drawable.speaker_on) speakerIcon!!.setIcon(R.drawable.speaker_on)
else
speakerIcon!!.setIcon(R.drawable.speaker_off)
} }
} }
} }
@@ -1856,7 +1856,6 @@ class MainActivity : AppCompatActivity() {
} }
} }
am.addOnModeChangedListener(mainExecutor, audioModeChangedListener!!) am.addOnModeChangedListener(mainExecutor, audioModeChangedListener!!)
Log.d(TAG, "Setting audio mode to MODE_IN_COMMUNICATION")
if (!BaresipService.requestAudioFocus(am, AudioAttributes.CONTENT_TYPE_SPEECH)) if (!BaresipService.requestAudioFocus(am, AudioAttributes.CONTENT_TYPE_SPEECH))
Toast.makeText( Toast.makeText(
applicationContext, applicationContext,
+17 -14
View File
@@ -892,7 +892,7 @@ object Utils {
fun isSpeakerPhoneOn(am: AudioManager): Boolean { fun isSpeakerPhoneOn(am: AudioManager): Boolean {
return if (Build.VERSION.SDK_INT >= 31) return if (Build.VERSION.SDK_INT >= 31)
am.communicationDevice!!.type != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
else else
am.isSpeakerphoneOn am.isSpeakerphoneOn
} }
@@ -903,12 +903,6 @@ object Utils {
Log.d(TAG, "Current com dev/mode is $current/${am.mode}") Log.d(TAG, "Current com dev/mode is $current/${am.mode}")
var speakerDevice: AudioDeviceInfo? = null var speakerDevice: AudioDeviceInfo? = null
if (enable) { if (enable) {
for (device in am.availableCommunicationDevices)
if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
speakerDevice = device
break
}
if (speakerDevice == null)
for (device in am.availableCommunicationDevices) for (device in am.availableCommunicationDevices)
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
speakerDevice = device speakerDevice = device
@@ -921,7 +915,11 @@ object Utils {
break break
} }
} }
if (speakerDevice != null && current != speakerDevice.type) { if (speakerDevice == null) {
Log.w(TAG,"Could not find requested communication device")
return
}
if (current != speakerDevice.type) {
if (speakerDevice.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { if (speakerDevice.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
am.clearCommunicationDevice() am.clearCommunicationDevice()
Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE") Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE")
@@ -942,8 +940,10 @@ object Utils {
override fun onModeChanged(mode: Int) { override fun onModeChanged(mode: Int) {
if (mode == AudioManager.MODE_NORMAL) { if (mode == AudioManager.MODE_NORMAL) {
am.removeOnModeChangedListener(this) am.removeOnModeChangedListener(this)
Log.d(TAG, "Setting com device to ${speakerDevice.type}" + Log.d(
" in mode ${am.mode}") TAG, "Setting com device to ${speakerDevice.type}" +
" in mode ${am.mode}"
)
if (!am.setCommunicationDevice(speakerDevice)) if (!am.setCommunicationDevice(speakerDevice))
Log.e(TAG, "Could not set com device") Log.e(TAG, "Could not set com device")
} }
@@ -963,11 +963,14 @@ object Utils {
} }
fun toggleSpeakerPhone(executor: Executor, am: AudioManager) { fun toggleSpeakerPhone(executor: Executor, am: AudioManager) {
if (Build.VERSION.SDK_INT >= 31) if (Build.VERSION.SDK_INT >= 31) {
setSpeakerPhone(executor, am, if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) setSpeakerPhone(executor, am, true)
else else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
setSpeakerPhone(executor, am, false)
} else {
setSpeakerPhone(executor, am, !am.isSpeakerphoneOn) setSpeakerPhone(executor, am, !am.isSpeakerphoneOn)
} }
}
} }