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
+126
@@ -0,0 +1,126 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.net.URI
|
||||
import javax.sound.sampled.AudioSystem
|
||||
import javax.sound.sampled.SourceDataLine
|
||||
import kotlin.concurrent.thread
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
/**
|
||||
* Desktop playback through the MP3SPI decoder and a PCM line. Pause/resume is a
|
||||
* decoder loop gate, so no audio device is reopened. Seeking is not supported.
|
||||
*/
|
||||
class DesktopAudioPlayer : AudioPlayer {
|
||||
|
||||
private var input: InputStream? = null
|
||||
private var line: SourceDataLine? = null
|
||||
private var worker: Thread? = null
|
||||
private val lock = Object()
|
||||
|
||||
@Volatile private var paused = false
|
||||
@Volatile private var stopped = false
|
||||
@Volatile private var finished = false
|
||||
@Volatile private var framesRead = 0L
|
||||
private var frameSize = 4
|
||||
private var frameRate = 44_100f
|
||||
private var duration = 0.0
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean) {
|
||||
stop()
|
||||
finished = false
|
||||
paused = false
|
||||
stopped = false
|
||||
framesRead = 0
|
||||
duration = song.duration.toDouble()
|
||||
|
||||
val raw = if (local) FileInputStream(File(source)) else URI(source).toURL().openStream()
|
||||
val buffered = BufferedInputStream(raw, 128 * 1024)
|
||||
input = buffered
|
||||
val audio = AudioSystem.getAudioInputStream(buffered)
|
||||
frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4
|
||||
frameRate = audio.format.frameRate.takeIf { it > 0 } ?: 44_100f
|
||||
|
||||
val info = javax.sound.sampled.DataLine.Info(SourceDataLine::class.java, audio.format)
|
||||
val dataLine = AudioSystem.getLine(info) as SourceDataLine
|
||||
dataLine.open(audio.format)
|
||||
dataLine.start()
|
||||
line = dataLine
|
||||
|
||||
worker = thread(isDaemon = true, name = "audiomanager-player") {
|
||||
val buffer = ByteArray(16 * 1024)
|
||||
try {
|
||||
while (!stopped) {
|
||||
synchronized(lock) {
|
||||
while (paused && !stopped) lock.wait()
|
||||
}
|
||||
if (stopped) break
|
||||
|
||||
val read = audio.read(buffer)
|
||||
if (read < 0) {
|
||||
finished = true
|
||||
break
|
||||
}
|
||||
dataLine.write(buffer, 0, read)
|
||||
framesRead += read.toLong()
|
||||
}
|
||||
dataLine.drain()
|
||||
} catch (_: Throwable) {
|
||||
// Stopping closes the stream; that is expected.
|
||||
} finally {
|
||||
runCatching { dataLine.close() }
|
||||
runCatching { audio.close() }
|
||||
runCatching { buffered.close() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
paused = true
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
synchronized(lock) {
|
||||
paused = false
|
||||
lock.notifyAll()
|
||||
}
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
stopped = true
|
||||
paused = false
|
||||
synchronized(lock) { lock.notifyAll() }
|
||||
runCatching { line?.stop() }
|
||||
runCatching { line?.flush() }
|
||||
worker?.interrupt()
|
||||
runCatching { worker?.join(400) }
|
||||
runCatching { input?.close() }
|
||||
line = null
|
||||
input = null
|
||||
worker = null
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean =
|
||||
!paused && !finished && worker?.isAlive == true
|
||||
|
||||
override fun isFinished(): Boolean = finished
|
||||
|
||||
override fun positionSeconds(): Double {
|
||||
val seconds = framesRead.toDouble() / frameSize / frameRate
|
||||
return seconds.coerceIn(0.0, duration.takeIf { it > 0 } ?: seconds)
|
||||
}
|
||||
|
||||
override fun durationSeconds(): Double = duration
|
||||
|
||||
override val canSeek: Boolean = false
|
||||
|
||||
override fun seekTo(seconds: Double) = Unit
|
||||
|
||||
override fun release() {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user