fix: Properly stop RecorderService onDestroy

This commit is contained in:
Myzel394 2023-10-21 19:37:17 +02:00
parent 5b7ce77ad3
commit 825f0eb33f
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
2 changed files with 50 additions and 28 deletions

View File

@ -19,28 +19,31 @@ class AudioRecorderService : IntervalRecorderService() {
val filePath: String
get() = "$folder/$counter.${settings!!.fileExtension}"
private fun _setAudioDevice() {
private fun clearAudioDevice() {
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (selectedMicrophone == null) {
audioManger.clearCommunicationDevice()
} else {
audioManger.setCommunicationDevice(selectedMicrophone!!.deviceInfo)
}
audioManger.clearCommunicationDevice()
} else {
if (selectedMicrophone == null) {
audioManger.stopBluetoothSco()
} else {
audioManger.startBluetoothSco()
}
audioManger.stopBluetoothSco()
}
}
private fun startAudioDevice() {
if (selectedMicrophone == null) {
return
}
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
audioManger.setCommunicationDevice(selectedMicrophone!!.deviceInfo)
} else {
audioManger.startBluetoothSco()
}
}
private fun createRecorder(): MediaRecorder {
_setAudioDevice()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(this)
} else {
@ -70,6 +73,7 @@ class AudioRecorderService : IntervalRecorderService() {
it.stop()
it.release()
}
clearAudioDevice()
}
}
@ -81,6 +85,7 @@ class AudioRecorderService : IntervalRecorderService() {
}
resetRecorder()
startAudioDevice()
try {
recorder = newRecorder

View File

@ -23,7 +23,7 @@ import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
abstract class RecorderService: Service() {
abstract class RecorderService : Service() {
private val binder = RecorderBinder()
private var isPaused: Boolean = false
@ -61,7 +61,7 @@ abstract class RecorderService: Service() {
return super.onStartCommand(intent, flags, startId)
}
inner class RecorderBinder: Binder() {
inner class RecorderBinder : Binder() {
fun getService(): RecorderService = this@RecorderService
}
@ -95,10 +95,12 @@ abstract class RecorderService: Service() {
start()
}
}
RecorderState.PAUSED -> {
pause()
isPaused = true
}
RecorderState.IDLE -> {
stop()
onDestroy()
@ -109,6 +111,7 @@ abstract class RecorderService: Service() {
RecorderState.RECORDING -> {
createRecordingTimeTimer()
}
RecorderState.PAUSED, RecorderState.IDLE -> {
recordingTimeTimer.shutdown()
}
@ -121,7 +124,7 @@ abstract class RecorderService: Service() {
RecorderState.PAUSED
).contains(newState) &&
PermissionHelper.hasGranted(this, android.Manifest.permission.POST_NOTIFICATIONS)
){
) {
val notification = buildNotification()
NotificationManagerCompat.from(this).notify(
NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID,
@ -145,22 +148,28 @@ abstract class RecorderService: Service() {
override fun onDestroy() {
super.onDestroy()
stop()
changeState(RecorderState.IDLE)
stopForeground(STOP_FOREGROUND_REMOVE)
NotificationManagerCompat.from(this).cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
NotificationManagerCompat.from(this)
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopSelf()
}
private fun buildStartNotification(): Notification = NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
.setSmallIcon(R.drawable.launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
private fun buildStartNotification(): Notification =
NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
.setSmallIcon(R.drawable.launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
private fun getNotificationChangeStateIntent(newState: RecorderState, requestCode: Int): PendingIntent {
private fun getNotificationChangeStateIntent(
newState: RecorderState,
requestCode: Int
): PendingIntent {
return PendingIntent.getService(
this,
requestCode,
@ -172,8 +181,11 @@ abstract class RecorderService: Service() {
)
}
private fun buildNotification(): Notification = when(state) {
RecorderState.RECORDING -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
private fun buildNotification(): Notification = when (state) {
RecorderState.RECORDING -> NotificationCompat.Builder(
this,
NotificationHelper.RECORDER_CHANNEL_ID
)
.setContentTitle(getString(R.string.ui_audioRecorder_state_recording_title))
.setContentText(getString(R.string.ui_audioRecorder_state_recording_description))
.setSmallIcon(R.drawable.launcher_foreground)
@ -211,7 +223,11 @@ abstract class RecorderService: Service() {
getNotificationChangeStateIntent(RecorderState.PAUSED, 2),
)
.build()
RecorderState.PAUSED -> NotificationCompat.Builder(this, NotificationHelper.RECORDER_CHANNEL_ID)
RecorderState.PAUSED -> NotificationCompat.Builder(
this,
NotificationHelper.RECORDER_CHANNEL_ID
)
.setContentTitle(getString(R.string.ui_audioRecorder_state_paused_title))
.setContentText(getString(R.string.ui_audioRecorder_state_paused_description))
.setSmallIcon(R.drawable.launcher_foreground)
@ -236,6 +252,7 @@ abstract class RecorderService: Service() {
getNotificationChangeStateIntent(RecorderState.RECORDING, 3),
)
.build()
else -> throw IllegalStateException("Invalid state passed to `buildNotification()`")
}
}