feat: add the Kotlin Multiplatform AudioManager client
A shared Compose Multiplatform client for Android, desktop and web on the native mood-first API. - Bearer login with persisted, restorable sessions. - Mood grid and full editor (create, settings, seeds, delete). - Server-driven radio sessions with automatic started/completed/skipped events and prefetching. - Add to current mood or choose another mood from the player. - Library search, original MP3 downloads, offline playback of downloads. - Offline cache of moods, songs and the last sync revision. - Platform audio: Media3 on Android, MP3SPI on desktop, HTML audio on web. - Platform storage: filesystem on Android/desktop, localStorage on web. - Desktop tests for API parsing, errors and cache persistence. - Gitea CI image with Android SDK 35, build pipeline and tagged releases.
This commit is contained in:
commit
3281e46104
40 files changed
+7021
No files matched your search
@@ -0,0 +1,43 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.toComposeImageBitmap
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.js.Js
|
||||
import kotlinx.browser.window
|
||||
import org.w3c.dom.Storage
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
actual fun platformEngine(): HttpClientEngine = Js.create()
|
||||
|
||||
actual val platformName: String = "web"
|
||||
|
||||
actual fun defaultServerUrl(): String = window.location.origin
|
||||
|
||||
actual fun platformStorage(): AppStorage = WebStorage()
|
||||
|
||||
actual fun createAudioPlayer(): AudioPlayer = WebAudioPlayer()
|
||||
|
||||
class WebStorage : AppStorage {
|
||||
private val storage: Storage get() = window.localStorage
|
||||
|
||||
override fun read(name: String): String? = storage.getItem("am:$name")
|
||||
override fun write(name: String, text: String) = storage.setItem("am:$name", text)
|
||||
override fun delete(name: String) = storage.removeItem("am:$name")
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? = null
|
||||
override fun downloadPath(songId: String): String? = null
|
||||
override fun listDownloads(): Map<String, Long> = emptyMap()
|
||||
override fun deleteDownload(songId: String) = Unit
|
||||
override fun storageUsedBytes(): Long = 0L
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) {
|
||||
// Browser back is not intercepted.
|
||||
}
|
||||
|
||||
actual fun decodeImage(bytes: ByteArray): ImageBitmap? =
|
||||
runCatching { org.jetbrains.skia.Image.makeFromEncoded(bytes).toComposeImageBitmap() }.getOrNull()
|
||||
@@ -0,0 +1,52 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import kotlinx.browser.document
|
||||
import org.w3c.dom.HTMLAudioElement
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
class WebAudioPlayer : AudioPlayer {
|
||||
|
||||
private val audio: HTMLAudioElement =
|
||||
(document.createElement("audio") as HTMLAudioElement).apply {
|
||||
preload = "auto"
|
||||
}
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean) {
|
||||
audio.src = source
|
||||
audio.play()
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
audio.pause()
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
audio.play()
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
audio.pause()
|
||||
audio.removeAttribute("src")
|
||||
audio.load()
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean = !audio.paused && !audio.ended
|
||||
|
||||
override fun isFinished(): Boolean = audio.ended
|
||||
|
||||
override fun positionSeconds(): Double = audio.currentTime.takeIf { it.isFinite() } ?: 0.0
|
||||
|
||||
override fun durationSeconds(): Double =
|
||||
audio.duration.takeIf { it.isFinite() && it > 0 } ?: 0.0
|
||||
|
||||
override val canSeek: Boolean = true
|
||||
|
||||
override fun seekTo(seconds: Double) {
|
||||
audio.currentTime = seconds
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
import androidx.compose.ui.window.ComposeViewport
|
||||
import kotlinx.browser.document
|
||||
import win.taptap.musicmanager.client.ui.App
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
fun main() {
|
||||
ComposeViewport(document.body!!) {
|
||||
App()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>AudioManager</title>
|
||||
<style>
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
background: #0d1117;
|
||||
overflow: hidden;
|
||||
}
|
||||
#composeApp {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
canvas { outline: none; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<script type="application/javascript" src="composeApp.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user