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
+76
@@ -0,0 +1,76 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.media3.common.AudioAttributes
|
||||
import androidx.media3.common.C
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import java.io.File
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
class AndroidAudioPlayer(context: Context) : AudioPlayer {
|
||||
|
||||
private val player = ExoPlayer.Builder(context).build().apply {
|
||||
setAudioAttributes(
|
||||
AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
true,
|
||||
)
|
||||
addListener(object : Player.Listener {
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
if (playbackState == Player.STATE_ENDED) finished = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private var finished = false
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean) {
|
||||
finished = false
|
||||
val uri = if (local) Uri.fromFile(File(source)) else Uri.parse(source)
|
||||
player.setMediaItem(MediaItem.fromUri(uri))
|
||||
player.prepare()
|
||||
player.play()
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
player.pause()
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
player.play()
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
player.stop()
|
||||
player.clearMediaItems()
|
||||
finished = false
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean = player.isPlaying
|
||||
|
||||
override fun isFinished(): Boolean = finished
|
||||
|
||||
override fun positionSeconds(): Double =
|
||||
player.currentPosition.coerceAtLeast(0L) / 1000.0
|
||||
|
||||
override fun durationSeconds(): Double {
|
||||
val duration = player.duration
|
||||
return if (duration > 0) duration / 1000.0 else 0.0
|
||||
}
|
||||
|
||||
override val canSeek: Boolean = true
|
||||
|
||||
override fun seekTo(seconds: Double) {
|
||||
player.seekTo((seconds * 1000).toLong().coerceAtLeast(0L))
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
player.release()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import android.content.Context
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.runtime.Composable
|
||||
import win.taptap.musicmanager.client.ui.App
|
||||
|
||||
object AndroidContextHolder {
|
||||
lateinit var context: Context
|
||||
}
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: android.os.Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
AndroidContextHolder.context = applicationContext
|
||||
enableEdgeToEdge()
|
||||
setContent { App() }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) {
|
||||
BackHandler(enabled = enabled, onBack = onBack)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import java.io.File
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
actual fun platformEngine(): HttpClientEngine = OkHttp.create()
|
||||
|
||||
actual val platformName: String = "android"
|
||||
|
||||
actual fun defaultServerUrl(): String = "http://10.0.2.2:8080"
|
||||
|
||||
actual fun platformStorage(): AppStorage = AndroidStorage(AndroidContextHolder.context)
|
||||
|
||||
actual fun createAudioPlayer(): AudioPlayer = AndroidAudioPlayer(AndroidContextHolder.context)
|
||||
|
||||
class AndroidStorage(context: Context) : AppStorage {
|
||||
|
||||
private val root = File(context.filesDir, "audiomanager").apply { mkdirs() }
|
||||
private val downloads = File(root, "downloads").apply { mkdirs() }
|
||||
|
||||
override fun read(name: String): String? {
|
||||
val file = File(root, name)
|
||||
return if (file.isFile) file.readText() else null
|
||||
}
|
||||
|
||||
override fun write(name: String, text: String) {
|
||||
root.mkdirs()
|
||||
File(root, name).writeText(text)
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
File(root, name).delete()
|
||||
}
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? {
|
||||
downloads.mkdirs()
|
||||
val file = File(downloads, "${safeName(songId)}.mp3")
|
||||
file.writeBytes(bytes)
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
override fun downloadPath(songId: String): String? =
|
||||
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath
|
||||
|
||||
override fun listDownloads(): Map<String, Long> =
|
||||
downloads.listFiles()
|
||||
?.filter { it.isFile && it.extension == "mp3" }
|
||||
?.associate { it.nameWithoutExtension to it.length() }
|
||||
?: emptyMap()
|
||||
|
||||
override fun deleteDownload(songId: String) {
|
||||
File(downloads, "${safeName(songId)}.mp3").delete()
|
||||
}
|
||||
|
||||
override fun storageUsedBytes(): Long =
|
||||
downloads.listFiles()?.sumOf { it.length() } ?: 0L
|
||||
|
||||
private fun safeName(songId: String): String =
|
||||
songId.map { if (it.isLetterOrDigit() || it == '-' || it == '_') it else '_' }.joinToString("")
|
||||
}
|
||||
|
||||
actual fun decodeImage(bytes: ByteArray): ImageBitmap? =
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)?.asImageBitmap()
|
||||
Reference in new issue
Block a user