[Feature]: Android app: music player

未關閉
#46 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
20/100
Issue 類型
功能
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
android, kotlin, react-native, rust, typescript

研究方向

Read the epic #40 first, then review #41 and #42 for the Android build plumbing and #47 for the music cache. Trace the web behavior through web/decoder/src/lib.rs, web/src/music/decoder.worker.ts, web/src/music/engine.ts, and web/src/music/player.ts; the issue names the mobile audio module and UI entry points as well. Done means meeting the playback, background controls, shared queue, web regression, and Android Opus acceptance criteria.

由索引模型根據 Issue 內容生成。

描述

enhancement

[!NOTE]
This issue was written by Claude (Claude Opus 5.5) from a planning session with @PhrolovaMoe. Part of #40, builds on #41 and #42. Read the epic first.

Area

Streaming / playback

Problem

The web music player is a custom engine that gives sample-accurate gapless playback, crossfade and ReplayGain:

  • a Rust decoder (web/decoder, Symphonia) compiled to WASM, run in a worker that reads files over synchronous Range XHRs,
  • crossfade/sequencing logic in web/src/music/decoder.worker.ts,
  • a SharedArrayBuffer ring played by an AudioWorklet.

None of that runs in React Native. On Android, music also has to keep playing with the screen off and be controllable from the lock screen, notification shade, Bluetooth and the OEM surfaces (Samsung Now Bar, HyperOS island, OPPO/OnePlus Live Alerts).

Proposed solution

Share the decoding and sequencing in Rust, run it natively behind a Media3 player inside a media service, and keep the queue logic in TypeScript shared with web.

1. Rust: one core, two frontends
  • New crate decoder/core (tinystream-decoder-core, rlib, no wasm deps), added to the Cargo workspace. It contains:
    • What web/decoder/src/lib.rs does today: probing, decoding, trimming encoder delay and padding, resampling with rubato to the output rate, stereo float output, gain, seek. The HTTP reading is abstracted behind a trait, e.g. trait RangeSource { fn len(&self) -> io::Result<u64>; fn read_range(&self, start: u64, end: u64) -> io::Result<Vec<u8>>; }. The block cache (BLOCK 512 KB, KEEP 12) stays in the core.
    • The sequencer now in decoder.worker.ts pump()/begin(): a list of items { key, source, size, ext, gain, crossfade }, decoding one into the next without gaps, crossfade mixing when crossfade > 0, skipping items that fail to open while reporting the error, and producing "mark" events (key, frame, offset, duration) and an "end" event. This makes gapless/crossfade logic exist once.
  • web/decoder becomes a thin wasm-bindgen frontend over the core, with the same JS API as today plus the sequencer. Simplify decoder.worker.ts to: create sources through tsReadRange, feed the sequencer, copy its output into the ring, and forward its events. The web player must behave exactly as before (gapless, crossfade, ReplayGain, error fallback to FLAC, seeking).
  • mobile/modules/audio/rust (tinystream-decoder-android): a uniffi frontend over the core, built by the #41 cargo-ndk plumbing.
    • RangeSource is implemented in Kotlin through a uniffi callback interface, so all network and file I/O goes through OkHttp with the app's network config (cleartext, user CAs, Bearer), and through the music cache (#47) when a file is cached.
  • Codecs stay as today (flac, mp3, aac, alac, isomp4, ogg/vorbis, wav, aiff, pcm). Opus: add an optional opus feature to the core (libopus through a Rust binding, Ogg Opus with pre-skip honoured so it stays gapless). Enable it for Android only, because #47 stores Opus. It's off for wasm, so the web bundle doesn't grow.
2. Native engine (mobile/modules/audio, Kotlin)
  • An output thread pulls from the sequencer into an AudioTrack (float PCM, stereo, at the device's native output rate from AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE, low-latency off, a buffer of a few hundred ms).
  • The position is derived from frames played (AudioTrack.getPlaybackHeadPosition) and the sequencer's marks, the same model as web/src/music/engine.ts.
  • The engine's JS API mirrors the web Engine class, so the queue code can drive either:
    • play(items, start), upcoming(after, items), gain(key, gain), stop(), pause()/resume()
    • events track(key) (audible now, not just decoded), ended, error(key, message), starved(bool), and the position
  • Wrap it in a Media3 SimpleBasePlayer inside a MediaSessionService (foreground service type mediaPlayback):
    • DefaultMediaNotificationProvider with artwork (authenticated cover URL through a custom BitmapLoader), title, artist and album
    • custom commands: like/star, shuffle, repeat
    • seeking from the notification and lock screen
    • this is what makes Now Bar / islands / Live Alerts, the lock screen and Bluetooth/headset buttons work, with no OEM-specific code
  • Media-session commands (next, previous, seek, play/pause from the notification or headset) are sent to JS as events. JS decides, exactly as for in-app taps. The foreground service keeps the process (and JS) alive while playing.
  • Audio focus: pause on loss, pause on transient loss and resume on regain, duck on "can duck". ACTION_AUDIO_BECOMING_NOISY (headphones unplugged) pauses.
3. Queue logic shared with web
  • Move the platform-independent parts of web/src/music/player.ts into packages/shared and use them from both apps:
    • gainOf (ReplayGain off/track/album/auto, album gain when an album plays in order, clipping guard)
    • fadeAfter (never crossfade consecutive same-album tracks)
    • item/upcoming/lineUp ordering (repeat, shuffle with shuffledCopy)
    • the server play-queue sync (save, restore, refreshFromServer, SavePlayQueue)
    • loudness measuring ahead (MeasureLoudness)
    • Played / NowPlaying reporting
    • followRemote (used by #48)
  • Storage and engine access are injected (localStorage vs device storage, web Engine vs native module).
  • Web behaviour must not change.
  • Sources: the same rules as player.ts:69 and :194-205. Codecs in DECODES play from /api/music/tracks/{id}/file; everything else, and any failure, falls back to /flac. With #47, a cached file is preferred.
  • The web queue is the server play queue (shared with Subsonic clients), and so is the app's. Opening the app resumes the queue the web left, and the other way round.
4. UI

The web is the spec: music/Library.tsx, routes/album.$id.tsx, artist.$id.tsx, playlist.$id.tsx, music/components.tsx, controls.tsx, Pill.tsx, NowPlaying.tsx, components/MusicSettings.tsx.

  • Music tab:
    • A segmented Albums / Artists / Songs / Playlists view with virtualised grids and lists.
    • Album, artist and playlist pages with hero Tilt (gyro) and the ambient tint (#43 coverTint).
    • Stars, playlists editing, and add-to-queue/play-next through long-press menus.
  • Mini-player (the web's pill): above the tab bar on every tab, tinted from the art.
    • Tap or swipe up expands to Now Playing. Swipe left/right goes to next/previous (dismissThreshold + tick).
    • It can be closed (swipe down) only when paused, like web.
    • It's hidden during video, clips and rooms, and comes back paused.
  • Now Playing (full screen, an expanding shared-element-style transition from the mini-player):
    • artwork with Tilt (touch and gyro) and glare, a scrubber with tick haptics every 10 s, and the transport controls
    • tabs: Lyrics (synced, auto-scrolling, tap a line to seek), Queue (drag to reorder with a tick per slot change, swipe to remove), Info (format, bitrate, sample rate, gain applied, source: file/flac/cache)
    • swipe down to collapse
  • Settings → Music (as web): ReplayGain mode, crossfade, and app passwords for Subsonic clients.
Acceptance criteria
  • A gapless album (e.g. a live album) plays with no gaps. Crossfade works between albums and never inside one. ReplayGain levels match the web for the same track and mode.
  • Music keeps playing with the screen off and the app swiped away from recents (until stopped). The notification, lock screen, Bluetooth buttons and Samsung Now Bar / HyperOS island show and control it.
  • Queue changes on the phone show up in the web UI's queue and the other way round.
  • The web music player still passes the same manual checks (gapless, crossfade, ReplayGain, FLAC fallback, seeking).
  • Opus files play on Android through the opus feature (needed by #47).
主要語言
TypeScript
星號
4
分支
1
平均合併
4 小時 49 分鐘
30 天內合併 PR
5

環境準備

  • 提供 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

tinystream-dev/tinystream 的其他 Issue

查看 tinystream-dev/tinystream 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。