MusicService: add seeking ability
This commit is contained in:
parent
32753f0ab7
commit
c6608eaa90
@ -50,6 +50,7 @@ interface MusicService {
|
|||||||
val artist: Flow<String?>
|
val artist: Flow<String?>
|
||||||
val album: Flow<String?>
|
val album: Flow<String?>
|
||||||
val albumArt: Flow<Bitmap?>
|
val albumArt: Flow<Bitmap?>
|
||||||
|
val duration: Flow<Long?>
|
||||||
|
|
||||||
val lastPlayerPackage: String?
|
val lastPlayerPackage: String?
|
||||||
|
|
||||||
@ -58,6 +59,7 @@ interface MusicService {
|
|||||||
fun pause()
|
fun pause()
|
||||||
fun play()
|
fun play()
|
||||||
fun togglePause()
|
fun togglePause()
|
||||||
|
fun seekTo(position: Long)
|
||||||
fun openPlayer(): PendingIntent?
|
fun openPlayer(): PendingIntent?
|
||||||
|
|
||||||
fun openPlayerChooser(context: Context)
|
fun openPlayerChooser(context: Context)
|
||||||
@ -310,6 +312,18 @@ internal class MusicServiceImpl(
|
|||||||
}
|
}
|
||||||
}.shareIn(scope, SharingStarted.WhileSubscribed(), 1)
|
}.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? {
|
private suspend fun loadBitmapFromUri(uri: Uri, size: Int): Bitmap? {
|
||||||
try {
|
try {
|
||||||
val request = ImageRequest.Builder(context)
|
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? {
|
override fun openPlayer(): PendingIntent? {
|
||||||
|
|
||||||
val controller = currentMediaController.replayCache.firstOrNull()
|
val controller = currentMediaController.replayCache.firstOrNull()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user