manage all audio in baresip service
This commit is contained in:
@ -39,6 +39,8 @@ class BaresipService: Service() {
|
||||
|
||||
internal var rtTimer: Timer? = null
|
||||
internal var filesPath = ""
|
||||
internal var audioFocusRequest: AudioFocusRequest? = null
|
||||
internal var audioFocused = false
|
||||
|
||||
override fun onCreate() {
|
||||
|
||||
@ -170,6 +172,12 @@ class BaresipService: Service() {
|
||||
updateStatusNotification()
|
||||
}
|
||||
|
||||
"ToggleSpeaker" -> {
|
||||
Log.d(LOG_TAG, "Toggling speakerphone from $speakerPhone")
|
||||
am.isSpeakerphoneOn = !am.isSpeakerphoneOn
|
||||
speakerPhone = am.isSpeakerphoneOn
|
||||
}
|
||||
|
||||
"Stop", "Stop Force" -> {
|
||||
if (action == "Stop") cleanService()
|
||||
if (isServiceRunning) baresipStop(action == "Stop Force")
|
||||
@ -335,11 +343,7 @@ class BaresipService: Service() {
|
||||
CallHistory.add(CallHistory(aor, call.peerURI, call.dir, true))
|
||||
CallHistory.save(filesPath)
|
||||
call.hasHistory = true
|
||||
if (rtTimer != null) {
|
||||
rtTimer!!.cancel()
|
||||
rtTimer = null
|
||||
if (rt.isPlaying) rt.stop()
|
||||
}
|
||||
stopRinging()
|
||||
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||
requestAudioFocus(AudioManager.STREAM_VOICE_CALL)
|
||||
am.isSpeakerphoneOn = false
|
||||
@ -537,13 +541,14 @@ class BaresipService: Service() {
|
||||
private fun requestAudioFocus(streamType: Int) {
|
||||
val res: Int
|
||||
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) && (audioFocusRequest == null)) {
|
||||
val playbackAttributes = AudioAttributes.Builder()
|
||||
.setLegacyStreamType(streamType)
|
||||
.build()
|
||||
@TargetApi(26)
|
||||
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
|
||||
.setAudioAttributes(playbackAttributes)
|
||||
.build()
|
||||
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
|
||||
setAudioAttributes(AudioAttributes.Builder().run {
|
||||
setLegacyStreamType(streamType)
|
||||
build()
|
||||
})
|
||||
build()
|
||||
}
|
||||
@TargetApi(26)
|
||||
res = am.requestAudioFocus(audioFocusRequest)
|
||||
if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
||||
@ -553,8 +558,7 @@ class BaresipService: Service() {
|
||||
audioFocusRequest = null
|
||||
}
|
||||
} else {
|
||||
res = am.requestAudioFocus(null, streamType,
|
||||
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
|
||||
res = am.requestAudioFocus(null, streamType, AudioManager.AUDIOFOCUS_GAIN)
|
||||
if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
||||
Log.d("Baresip", "Audio focus granted")
|
||||
audioFocused = true
|
||||
@ -598,6 +602,7 @@ class BaresipService: Service() {
|
||||
rtTimer!!.cancel()
|
||||
rtTimer = null
|
||||
if (rt.isPlaying) rt.stop()
|
||||
abandonAudioFocus()
|
||||
}
|
||||
}
|
||||
|
||||
@ -640,8 +645,7 @@ class BaresipService: Service() {
|
||||
var contacts = ArrayList<Contact>()
|
||||
var chatTexts: MutableMap<String, String> = mutableMapOf<String, String>()
|
||||
|
||||
var audioFocused = false
|
||||
var audioFocusRequest: AudioFocusRequest? = null
|
||||
var speakerPhone = false
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -13,7 +13,6 @@ import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.media.*
|
||||
import android.os.CountDownTimer
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import android.support.v7.view.menu.ActionMenuItemView
|
||||
@ -47,8 +46,6 @@ class MainActivity : AppCompatActivity() {
|
||||
internal lateinit var infoButton: ImageButton
|
||||
internal lateinit var uaAdapter: UaSpinnerAdapter
|
||||
internal lateinit var aorSpinner: Spinner
|
||||
internal lateinit var am: AudioManager
|
||||
internal lateinit var rt: Ringtone
|
||||
internal lateinit var imm: InputMethodManager
|
||||
internal lateinit var nm: NotificationManager
|
||||
internal lateinit var serviceEventReceiver: BroadcastReceiver
|
||||
@ -85,10 +82,6 @@ class MainActivity : AppCompatActivity() {
|
||||
callsButton = findViewById(R.id.callsButton) as ImageButton
|
||||
dialpadButton = findViewById(R.id.dialpadButton) as ImageButton
|
||||
|
||||
am = getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
val rtUri = RingtoneManager.getActualDefaultRingtoneUri(applicationContext,
|
||||
RingtoneManager.TYPE_RINGTONE)
|
||||
rt = RingtoneManager.getRingtone(applicationContext, rtUri)
|
||||
imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
@ -834,13 +827,14 @@ class MainActivity : AppCompatActivity() {
|
||||
val i: Intent
|
||||
when (item.itemId) {
|
||||
R.id.speakerIcon -> {
|
||||
am.isSpeakerphoneOn = !am.isSpeakerphoneOn
|
||||
val speakerIcon = findViewById(R.id.speakerIcon) as ActionMenuItemView
|
||||
if (am.isSpeakerphoneOn)
|
||||
speakerIcon.setBackgroundColor(Color.rgb(0x04, 0xb4, 0x04))
|
||||
else
|
||||
if (BaresipService.speakerPhone)
|
||||
speakerIcon.setBackgroundColor(ContextCompat.getColor(applicationContext,
|
||||
R.color.colorPrimary))
|
||||
else
|
||||
speakerIcon.setBackgroundColor(Color.rgb(0x04, 0xb4, 0x04))
|
||||
baresipService.setAction("ToggleSpeaker")
|
||||
startService(baresipService)
|
||||
return true
|
||||
}
|
||||
R.id.config -> {
|
||||
|
||||
Reference in New Issue
Block a user