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