minor fixes

This commit is contained in:
Thibault Deckers 2023-02-17 16:38:50 +01:00
parent d319794a50
commit 6a8cc1ba0c
2 changed files with 49 additions and 34 deletions

View file

@ -37,12 +37,16 @@ class AnalysisService : Service() {
} }
} }
initChannels(this) try {
initChannels(this)
HandlerThread("Analysis service handler", Process.THREAD_PRIORITY_BACKGROUND).apply { HandlerThread("Analysis service handler", Process.THREAD_PRIORITY_BACKGROUND).apply {
start() start()
serviceLooper = looper serviceLooper = looper
serviceHandler = ServiceHandler(looper) serviceHandler = ServiceHandler(looper)
}
} catch (e: Exception) {
Log.e(LOG_TAG, "failed to initialize service", e)
} }
} }

View file

@ -35,6 +35,15 @@ class MediaSessionHandler(private val context: Context, private val mediaCommand
} }
fun dispose() { fun dispose() {
unregisterNoisyAudioReceiver()
}
private fun registerNoisyAudioReceiver() {
context.registerReceiver(noisyAudioReceiver, IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY))
isNoisyAudioReceiverRegistered = true
}
private fun unregisterNoisyAudioReceiver() {
if (isNoisyAudioReceiverRegistered) { if (isNoisyAudioReceiverRegistered) {
context.unregisterReceiver(noisyAudioReceiver) context.unregisterReceiver(noisyAudioReceiver)
isNoisyAudioReceiverRegistered = false isNoisyAudioReceiverRegistered = false
@ -70,7 +79,7 @@ class MediaSessionHandler(private val context: Context, private val mediaCommand
STATE_PAUSED -> PlaybackStateCompat.STATE_PAUSED STATE_PAUSED -> PlaybackStateCompat.STATE_PAUSED
STATE_PLAYING -> PlaybackStateCompat.STATE_PLAYING STATE_PLAYING -> PlaybackStateCompat.STATE_PLAYING
else -> { else -> {
result.error("update-state", "unknown state=$stateString", null) result.error("updateSession-state", "unknown state=$stateString", null)
return return
} }
} }
@ -93,39 +102,41 @@ class MediaSessionHandler(private val context: Context, private val mediaCommand
.build() .build()
FlutterUtils.runOnUiThread { FlutterUtils.runOnUiThread {
if (session == null) { try {
val mbrIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY_PAUSE) if (session == null) {
val mbrName = ComponentName(context, MediaButtonReceiver::class.java) val mbrIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY_PAUSE)
session = MediaSessionCompat(context, "aves", mbrName, mbrIntent).apply { val mbrName = ComponentName(context, MediaButtonReceiver::class.java)
setCallback(mediaCommandHandler) session = MediaSessionCompat(context, "aves", mbrName, mbrIntent).apply {
setCallback(mediaCommandHandler)
}
} }
} session!!.apply {
session!!.apply { val metadata = MediaMetadataCompat.Builder()
val metadata = MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title) .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, title) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, durationMillis)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, durationMillis) .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI, uri.toString())
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI, uri.toString()) .build()
.build() setMetadata(metadata)
setMetadata(metadata) setPlaybackState(playbackState)
setPlaybackState(playbackState) if (!isActive) {
if (!isActive) { isActive = true
isActive = true }
} }
}
val isPlaying = state == PlaybackStateCompat.STATE_PLAYING val isPlaying = state == PlaybackStateCompat.STATE_PLAYING
if (!wasPlaying && isPlaying) { if (!wasPlaying && isPlaying) {
context.registerReceiver(noisyAudioReceiver, IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) registerNoisyAudioReceiver()
isNoisyAudioReceiverRegistered = true } else if (wasPlaying && !isPlaying) {
} else if (wasPlaying && !isPlaying) { unregisterNoisyAudioReceiver()
context.unregisterReceiver(noisyAudioReceiver) }
isNoisyAudioReceiverRegistered = false wasPlaying = isPlaying
result.success(null)
} catch (e: Exception) {
result.error("updateSession-exception", e.message, e.stackTraceToString())
} }
wasPlaying = isPlaying
} }
result.success(null)
} }
private fun releaseSession(@Suppress("unused_parameter") call: MethodCall, result: MethodChannel.Result) { private fun releaseSession(@Suppress("unused_parameter") call: MethodCall, result: MethodChannel.Result) {