Minor AudioManager related improvements
This commit is contained in:
@@ -33,6 +33,7 @@ import java.util.*
|
|||||||
import kotlin.concurrent.schedule
|
import kotlin.concurrent.schedule
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
|
||||||
class BaresipService: Service() {
|
class BaresipService: Service() {
|
||||||
|
|
||||||
internal lateinit var intent: Intent
|
internal lateinit var intent: Intent
|
||||||
@@ -67,6 +68,8 @@ class BaresipService: Service() {
|
|||||||
filesPath = filesDir.absolutePath
|
filesPath = filesDir.absolutePath
|
||||||
|
|
||||||
am = getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
am = getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||||
|
// Hack to avoid playback delay when outgoing call is established
|
||||||
|
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||||
|
|
||||||
val rtUri = RingtoneManager.getActualDefaultRingtoneUri(applicationContext,
|
val rtUri = RingtoneManager.getActualDefaultRingtoneUri(applicationContext,
|
||||||
RingtoneManager.TYPE_RINGTONE)
|
RingtoneManager.TYPE_RINGTONE)
|
||||||
@@ -511,7 +514,8 @@ class BaresipService: Service() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
"call progress", "call ringing" -> {
|
"call progress", "call ringing" -> {
|
||||||
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION)
|
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
|
||||||
|
AudioAttributes.CONTENT_TYPE_SPEECH)
|
||||||
setCallVolume()
|
setCallVolume()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -602,6 +606,7 @@ class BaresipService: Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"call answered" -> {
|
"call answered" -> {
|
||||||
|
stopRinging()
|
||||||
proximitySensing(true)
|
proximitySensing(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -620,8 +625,8 @@ class BaresipService: Service() {
|
|||||||
CallHistory.save()
|
CallHistory.save()
|
||||||
call.hasHistory = true
|
call.hasHistory = true
|
||||||
}
|
}
|
||||||
stopRinging()
|
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION,
|
||||||
requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION)
|
AudioAttributes.CONTENT_TYPE_SPEECH)
|
||||||
setCallVolume()
|
setCallVolume()
|
||||||
if (!Utils.isVisible())
|
if (!Utils.isVisible())
|
||||||
return
|
return
|
||||||
@@ -939,7 +944,7 @@ class BaresipService: Service() {
|
|||||||
return spannable
|
return spannable
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun requestAudioFocus(usage: Int) {
|
private fun requestAudioFocus(usage: Int, type: Int) {
|
||||||
if (audioFocusUsage != -1) {
|
if (audioFocusUsage != -1) {
|
||||||
if (audioFocusUsage == usage)
|
if (audioFocusUsage == usage)
|
||||||
return
|
return
|
||||||
@@ -952,6 +957,7 @@ class BaresipService: Service() {
|
|||||||
.run {
|
.run {
|
||||||
setAudioAttributes(AudioAttributes.Builder().run {
|
setAudioAttributes(AudioAttributes.Builder().run {
|
||||||
setUsage(usage)
|
setUsage(usage)
|
||||||
|
setContentType(type)
|
||||||
build()
|
build()
|
||||||
})
|
})
|
||||||
build()
|
build()
|
||||||
@@ -1021,7 +1027,8 @@ class BaresipService: Service() {
|
|||||||
|
|
||||||
private fun startRinging() {
|
private fun startRinging() {
|
||||||
am.mode = AudioManager.MODE_RINGTONE
|
am.mode = AudioManager.MODE_RINGTONE
|
||||||
requestAudioFocus(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
|
requestAudioFocus(AudioAttributes.USAGE_NOTIFICATION_RINGTONE,
|
||||||
|
AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||||
if (VERSION.SDK_INT >= 28) {
|
if (VERSION.SDK_INT >= 28) {
|
||||||
rt.isLooping = true
|
rt.isLooping = true
|
||||||
rt.play()
|
rt.play()
|
||||||
@@ -1034,12 +1041,13 @@ class BaresipService: Service() {
|
|||||||
rt.play()
|
rt.play()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1000 * 1, 1000 * 1)
|
}, 1000, 1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopRinging() {
|
private fun stopRinging() {
|
||||||
if (am.mode == AudioManager.MODE_RINGTONE) {
|
if (am.mode == AudioManager.MODE_RINGTONE) {
|
||||||
|
abandonAudioFocus()
|
||||||
if ((VERSION.SDK_INT < 28) && (rtTimer != null)) {
|
if ((VERSION.SDK_INT < 28) && (rtTimer != null)) {
|
||||||
rtTimer!!.cancel()
|
rtTimer!!.cancel()
|
||||||
rtTimer = null
|
rtTimer = null
|
||||||
|
|||||||
@@ -644,12 +644,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
delegate.applyDayNight()
|
delegate.applyDayNight()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (am.mode == AudioManager.MODE_RINGTONE) {
|
if (am.mode == AudioManager.MODE_RINGTONE)
|
||||||
volumeControlStream = AudioManager.STREAM_RING
|
volumeControlStream = AudioManager.STREAM_RING
|
||||||
} else {
|
else
|
||||||
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
|
||||||
volumeControlStream = AudioManager.STREAM_VOICE_CALL
|
volumeControlStream = AudioManager.STREAM_VOICE_CALL
|
||||||
}
|
|
||||||
|
|
||||||
} // OnCreate
|
} // OnCreate
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user