Skip to content

Commit

Permalink
fix: Properly stop foreground service
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Aug 17, 2023
1 parent 3e41030 commit 8dcefc5
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class AudioRecorderModel: ViewModel() {
val progress: Float
get() = (recordingTime!! / recorderService!!.settings!!.maxDuration).toFloat()

private var intent: Intent? = null
var recorderService: AudioRecorderService? = null
private set

Expand Down Expand Up @@ -86,15 +85,13 @@ class AudioRecorderModel: ViewModel() {
}

fun startRecording(context: Context) {
reset()

runCatching {
context.unbindService(connection)
}

intent = Intent(context, AudioRecorderService::class.java)
ContextCompat.startForegroundService(context, intent!!)
context.bindService(intent!!, connection, Context.BIND_AUTO_CREATE)
val intent = Intent(context, AudioRecorderService::class.java)
ContextCompat.startForegroundService(context, intent)
context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
}

fun stopRecording(context: Context, saveAsLastRecording: Boolean = true) {
Expand All @@ -104,9 +101,11 @@ class AudioRecorderModel: ViewModel() {

runCatching {
context.unbindService(connection)
context.stopService(intent)
}

val intent = Intent(context, AudioRecorderService::class.java)
context.stopService(intent)

reset()
}

Expand Down

0 comments on commit 8dcefc5

Please sign in to comment.