Skip to content

Commit 4f37a2d

Browse files
committed
fix: add debounce and stop the loop
1 parent b12cd84 commit 4f37a2d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ android {
4747
applicationId = "com.ilseon"
4848
minSdk = 24
4949
targetSdk = 36
50-
versionCode = 128
51-
versionName = "0.41.3"
50+
versionCode = 129
51+
versionName = "0.41.4"
5252

5353
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
5454
}

app/src/main/java/com/ilseon/service/RecordingService.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class RecordingService : MediaBrowserService() {
6767
private var audioFocusRequest: AudioFocusRequest? = null
6868

6969
private var isScoStarted = false
70+
private var lastMediaButtonClickTime = 0L
71+
private val MEDIA_BUTTON_DEBOUNCE_MS = 1500L
7072

7173
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
7274

@@ -144,15 +146,19 @@ class RecordingService : MediaBrowserService() {
144146
.build()
145147
session.setMetadata(metadata)
146148

147-
val speed = if (mediaRecorder != null) 0f else 1f
149+
// Use STATE_PAUSED when idle so the system doesn't think we're
150+
// actively playing — STATE_PLAYING triggers auto-resume / event re-dispatch.
151+
val isRecording = mediaRecorder != null
152+
val state = if (isRecording) PlaybackState.STATE_PLAYING else PlaybackState.STATE_PAUSED
153+
val speed = if (isRecording) 1f else 0f
148154
val stateBuilder = PlaybackState.Builder()
149155
.setActions(
150156
PlaybackState.ACTION_PLAY or
151157
PlaybackState.ACTION_PAUSE or
152158
PlaybackState.ACTION_PLAY_PAUSE or
153159
PlaybackState.ACTION_STOP
154160
)
155-
.setState(PlaybackState.STATE_PLAYING, PlaybackState.PLAYBACK_POSITION_UNKNOWN, speed)
161+
.setState(state, PlaybackState.PLAYBACK_POSITION_UNKNOWN, speed)
156162
session.setPlaybackState(stateBuilder.build())
157163

158164
if (mediaRecorder == null) {
@@ -251,6 +257,13 @@ class RecordingService : MediaBrowserService() {
251257
}
252258

253259
private fun handleMediaButtonClick() {
260+
val now = System.currentTimeMillis()
261+
if (now - lastMediaButtonClickTime < MEDIA_BUTTON_DEBOUNCE_MS) {
262+
Log.d("RecordingService", "handleMediaButtonClick: debounced (${now - lastMediaButtonClickTime}ms since last)")
263+
return
264+
}
265+
lastMediaButtonClickTime = now
266+
254267
if (mediaRecorder == null) {
255268
hapticManager.performNudge()
256269
startRecording()
@@ -354,11 +367,12 @@ class RecordingService : MediaBrowserService() {
354367
}
355368

356369
private fun updatePlaybackState(isRecording: Boolean) {
357-
val speed = if (isRecording) 0f else 1f
370+
val state = if (isRecording) PlaybackState.STATE_PLAYING else PlaybackState.STATE_PAUSED
371+
val speed = if (isRecording) 1f else 0f
358372
mediaSession?.setPlaybackState(
359373
PlaybackState.Builder()
360374
.setActions(PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_PLAY_PAUSE)
361-
.setState(PlaybackState.STATE_PLAYING, PlaybackState.PLAYBACK_POSITION_UNKNOWN, speed)
375+
.setState(state, PlaybackState.PLAYBACK_POSITION_UNKNOWN, speed)
362376
.build()
363377
)
364378
}

0 commit comments

Comments
 (0)