Audio routing fixes
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
@@ -904,16 +904,10 @@ object Utils {
|
|||||||
var speakerDevice: AudioDeviceInfo? = null
|
var speakerDevice: AudioDeviceInfo? = null
|
||||||
if (enable) {
|
if (enable) {
|
||||||
for (device in am.availableCommunicationDevices)
|
for (device in am.availableCommunicationDevices)
|
||||||
if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
|
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
|
||||||
speakerDevice = device
|
speakerDevice = device
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (speakerDevice == null)
|
|
||||||
for (device in am.availableCommunicationDevices)
|
|
||||||
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
|
|
||||||
speakerDevice = device
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
for (device in am.availableCommunicationDevices)
|
for (device in am.availableCommunicationDevices)
|
||||||
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
|
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
|
||||||
@@ -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")
|
||||||
@@ -933,7 +931,7 @@ object Utils {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Currently at API levels 31+, speakerphone needs normal mode
|
// 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")
|
Log.d(TAG, "Setting com device to ${speakerDevice.type} in MODE_NORMAL")
|
||||||
if (!am.setCommunicationDevice(speakerDevice))
|
if (!am.setCommunicationDevice(speakerDevice))
|
||||||
Log.e(TAG, "Could not set com device")
|
Log.e(TAG, "Could not set com device")
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user