MusicService: add seeking ability

This commit is contained in:
MM20 2023-02-12 23:31:56 +01:00
parent 32753f0ab7
commit c6608eaa90
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -50,6 +50,7 @@ interface MusicService {
val artist: Flow<String?>
val album: Flow<String?>
val albumArt: Flow<Bitmap?>
val duration: Flow<Long?>
val lastPlayerPackage: String?
@ -58,6 +59,7 @@ interface MusicService {
fun pause()
fun play()
fun togglePause()
fun seekTo(position: Long)
fun openPlayer(): PendingIntent?
fun openPlayerChooser(context: Context)
@ -310,6 +312,18 @@ internal class MusicServiceImpl(
}
}.shareIn(scope, SharingStarted.WhileSubscribed(), 1)
override val duration: Flow<Long?> = channelFlow {
currentMetadata.collectLatest { metadata ->
if (metadata == null) {
send(null)
return@collectLatest
}
val duration = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION)
send(duration.takeIf { it > 0 })
}
}.shareIn(scope, SharingStarted.WhileSubscribed(), 1)
private suspend fun loadBitmapFromUri(uri: Uri, size: Int): Bitmap? {
try {
val request = ImageRequest.Builder(context)
@ -419,6 +433,13 @@ internal class MusicServiceImpl(
}
}
override fun seekTo(position: Long) {
scope.launch {
val controller = currentMediaController.firstOrNull()
controller?.transportControls?.seekTo(position)
}
}
override fun openPlayer(): PendingIntent? {
val controller = currentMediaController.replayCache.firstOrNull()