feat: service-owned queue, full player, audio consistency and mood settings
- Android Media3 service owns a real timeline: gapless playback, notification and headset next/back, 2 s previous rule, queue survives task swipes - per-track loudness gain via an Android PCM processor and desktop mixer - optional 0-8 s fade between tracks; full-screen player with quality info - per-mood offline switch auto-syncs songs on Wi-Fi, three at a time - dedicated mood settings screen with delete at the bottom - one-tap add-to-mood in the player; sleep timer can finish the current track - version 0.4.0
This commit is contained in:
1 parent
8d7f6a0ccb
commit
1e3dac4e47
26 files changed
+2088
-689
No files matched your search
+89
-43
@@ -10,8 +10,8 @@ import androidx.media3.common.MediaMetadata
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.session.MediaController
|
||||
import androidx.media3.session.SessionToken
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
import win.taptap.musicmanager.client.playback.PlaybackTrack
|
||||
|
||||
/**
|
||||
* Client side of [PlaybackService]. The controller is created asynchronously;
|
||||
@@ -55,43 +55,56 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
)
|
||||
}
|
||||
|
||||
override fun play(
|
||||
source: String,
|
||||
song: Song,
|
||||
local: Boolean,
|
||||
artworkUri: String?,
|
||||
artworkBytes: ByteArray?,
|
||||
) {
|
||||
override fun setQueue(tracks: List<PlaybackTrack>, startIndex: Int, startAtSeconds: Double) {
|
||||
if (tracks.isEmpty()) return
|
||||
finished = false
|
||||
withController { controller ->
|
||||
val uri = if (local) Uri.fromFile(java.io.File(source)) else Uri.parse(source)
|
||||
val metadata = MediaMetadata.Builder()
|
||||
.setTitle(song.title)
|
||||
.setArtist(song.artist)
|
||||
.setAlbumTitle(song.album)
|
||||
.apply {
|
||||
if (artworkBytes != null && artworkBytes.isNotEmpty()) {
|
||||
setArtworkData(
|
||||
artworkBytes,
|
||||
MediaMetadata.PICTURE_TYPE_FRONT_COVER,
|
||||
)
|
||||
} else {
|
||||
setArtworkUri(artworkUri?.let(Uri::parse))
|
||||
}
|
||||
}
|
||||
.build()
|
||||
val item = MediaItem.Builder()
|
||||
.setUri(uri)
|
||||
.setMimeType("audio/mpeg")
|
||||
.setMediaMetadata(metadata)
|
||||
.build()
|
||||
|
||||
controller.setMediaItem(item)
|
||||
controller.prepare()
|
||||
controller.play()
|
||||
val items = tracks.map(::mediaItem)
|
||||
val index = startIndex.coerceIn(tracks.indices)
|
||||
val positionMs = (startAtSeconds * 1_000).toLong().coerceAtLeast(0L)
|
||||
withController { current ->
|
||||
current.setMediaItems(items, index, positionMs)
|
||||
current.prepare()
|
||||
current.play()
|
||||
}
|
||||
}
|
||||
|
||||
override fun appendToQueue(tracks: List<PlaybackTrack>) {
|
||||
if (tracks.isEmpty()) return
|
||||
val items = tracks.map(::mediaItem)
|
||||
withController { current -> current.addMediaItems(items) }
|
||||
}
|
||||
|
||||
override fun moveInQueue(fromIndex: Int, toIndex: Int) {
|
||||
withController { current -> current.moveMediaItem(fromIndex, toIndex) }
|
||||
}
|
||||
|
||||
override fun removeAt(index: Int) {
|
||||
withController { current -> current.removeMediaItem(index) }
|
||||
}
|
||||
|
||||
override fun playIndex(index: Int, startAtSeconds: Double) {
|
||||
finished = false
|
||||
val positionMs = (startAtSeconds * 1_000).toLong().coerceAtLeast(0L)
|
||||
withController { current ->
|
||||
if (index in 0 until current.mediaItemCount) {
|
||||
current.seekTo(index, positionMs)
|
||||
current.play()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun skipToNext() {
|
||||
withController { current -> current.seekToNextMediaItem() }
|
||||
}
|
||||
|
||||
override fun skipToPrevious(restartAfterSeconds: Double) {
|
||||
withController { current -> current.seekToPreviousMediaItem() }
|
||||
}
|
||||
|
||||
override fun currentIndex(): Int = controller?.currentMediaItemIndex ?: 0
|
||||
|
||||
override fun queueSize(): Int = controller?.mediaItemCount ?: 0
|
||||
|
||||
override fun pause() {
|
||||
withController { it.pause() }
|
||||
}
|
||||
@@ -126,19 +139,26 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
withController { it.seekTo((seconds * 1000).toLong().coerceAtLeast(0L)) }
|
||||
}
|
||||
|
||||
override fun setSkipCallbacks(onNext: (() -> Unit)?, onPrevious: (() -> Unit)?) {
|
||||
PlaybackCommands.onNext = onNext
|
||||
PlaybackCommands.onPrevious = onPrevious
|
||||
override val supportsCrossfade: Boolean = true
|
||||
|
||||
override fun setCrossfadeSeconds(seconds: Int) {
|
||||
val args = Bundle().apply { putInt(CROSSFADE_SECONDS, seconds.coerceAtLeast(0)) }
|
||||
withController { current ->
|
||||
runCatching { current.sendCustomCommand(CROSSFADE_COMMAND, args) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun scheduleSleepTimer(seconds: Long?): Boolean {
|
||||
val args = Bundle().apply { putLong(SLEEP_TIMER_SECONDS, seconds ?: 0L) }
|
||||
val send: (MediaController) -> Boolean = { current ->
|
||||
runCatching { current.sendCustomCommand(SLEEP_TIMER_COMMAND, args) }.isSuccess
|
||||
val send: (MediaController) -> Unit = { current ->
|
||||
runCatching { current.sendCustomCommand(SLEEP_TIMER_COMMAND, args) }
|
||||
}
|
||||
val current = controller
|
||||
if (current != null && connected) return send(current)
|
||||
synchronized(pending) { pending.addLast { controller -> send(controller) } }
|
||||
if (current != null && connected) {
|
||||
send(current)
|
||||
return true
|
||||
}
|
||||
synchronized(pending) { pending.addLast(send) }
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -150,13 +170,39 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
synchronized(pending) { pending.clear() }
|
||||
}
|
||||
|
||||
private fun mediaItem(track: PlaybackTrack): MediaItem {
|
||||
val uri = if (track.local) {
|
||||
Uri.fromFile(java.io.File(track.source))
|
||||
} else {
|
||||
Uri.parse(track.source)
|
||||
}
|
||||
val metadata = MediaMetadata.Builder()
|
||||
.setTitle(track.song.title)
|
||||
.setArtist(track.song.artist)
|
||||
.setAlbumTitle(track.song.album)
|
||||
.setExtras(
|
||||
Bundle().apply { putDouble(GAIN_DB_KEY, track.gainDb ?: 0.0) },
|
||||
)
|
||||
.apply {
|
||||
if (!track.artworkUri.isNullOrBlank()) {
|
||||
setArtworkUri(Uri.parse(track.artworkUri))
|
||||
}
|
||||
}
|
||||
.build()
|
||||
return MediaItem.Builder()
|
||||
.setUri(uri)
|
||||
.setMimeType("audio/mpeg")
|
||||
.setMediaMetadata(metadata)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun withController(block: (MediaController) -> Unit) {
|
||||
val current = controller
|
||||
if (current != null && connected) {
|
||||
block(current)
|
||||
} else {
|
||||
// The first playback request can be followed immediately by a seek
|
||||
// (long-form resume). Keep every action, not only the last one.
|
||||
// The first playback request can be followed immediately by a seek.
|
||||
// Keep every action, not only the last one.
|
||||
synchronized(pending) { pending.addLast(block) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user