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

View File

@ -1491,6 +1491,7 @@ class BaresipService: Service() {
private var btAdapter: BluetoothAdapter? = null
fun requestAudioFocus(am: AudioManager, type: Int): Boolean {
Log.d(TAG, "Requesting audio focus of type $type")
if (audioFocusRequest != null) {
if (audioFocusRequest!!.audioAttributesCompat.contentType == type)
return true
@ -1515,7 +1516,7 @@ class BaresipService: Service() {
if (type == AudioAttributes.CONTENT_TYPE_SPEECH)
am.mode = AudioManager.MODE_IN_COMMUNICATION
} else {
Log.d(TAG, "Audio focus denied")
Log.i(TAG, "Audio focus denied")
audioFocusRequest = null
isAudioFocused = false
}

View File

@ -197,10 +197,10 @@ class MainActivity : AppCompatActivity() {
if (device != null) {
Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}")
if (speakerIcon != null) {
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
speakerIcon!!.setIcon(R.drawable.speaker_off)
else
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
speakerIcon!!.setIcon(R.drawable.speaker_on)
else
speakerIcon!!.setIcon(R.drawable.speaker_off)
}
}
}
@ -1856,7 +1856,6 @@ class MainActivity : AppCompatActivity() {
}
}
am.addOnModeChangedListener(mainExecutor, audioModeChangedListener!!)
Log.d(TAG, "Setting audio mode to MODE_IN_COMMUNICATION")
if (!BaresipService.requestAudioFocus(am, AudioAttributes.CONTENT_TYPE_SPEECH))
Toast.makeText(
applicationContext,

View File

@ -892,7 +892,7 @@ object Utils {
fun isSpeakerPhoneOn(am: AudioManager): Boolean {
return if (Build.VERSION.SDK_INT >= 31)
am.communicationDevice!!.type != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
else
am.isSpeakerphoneOn
}
@ -904,16 +904,10 @@ object Utils {
var speakerDevice: AudioDeviceInfo? = null
if (enable) {
for (device in am.availableCommunicationDevices)
if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
speakerDevice = device
break
}
if (speakerDevice == null)
for (device in am.availableCommunicationDevices)
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
speakerDevice = device
break
}
} else {
for (device in am.availableCommunicationDevices)
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
@ -921,7 +915,11 @@ object Utils {
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) {
am.clearCommunicationDevice()
Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE")
@ -933,7 +931,7 @@ object Utils {
}
} else {
// Currently at API levels 31+, speakerphone needs normal mode
if (am.mode == AudioManager.MODE_NORMAL) {
if (am.mode == AudioManager.MODE_NORMAL) {
Log.d(TAG, "Setting com device to ${speakerDevice.type} in MODE_NORMAL")
if (!am.setCommunicationDevice(speakerDevice))
Log.e(TAG, "Could not set com device")
@ -942,8 +940,10 @@ object Utils {
override fun onModeChanged(mode: Int) {
if (mode == AudioManager.MODE_NORMAL) {
am.removeOnModeChangedListener(this)
Log.d(TAG, "Setting com device to ${speakerDevice.type}" +
" in mode ${am.mode}")
Log.d(
TAG, "Setting com device to ${speakerDevice.type}" +
" in mode ${am.mode}"
)
if (!am.setCommunicationDevice(speakerDevice))
Log.e(TAG, "Could not set com device")
}
@ -963,11 +963,14 @@ object Utils {
}
fun toggleSpeakerPhone(executor: Executor, am: AudioManager) {
if (Build.VERSION.SDK_INT >= 31)
setSpeakerPhone(executor, am,
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
else
if (Build.VERSION.SDK_INT >= 31) {
if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
setSpeakerPhone(executor, am, true)
else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
setSpeakerPhone(executor, am, false)
} else {
setSpeakerPhone(executor, am, !am.isSpeakerphoneOn)
}
}
}