Skip to content

Open-source music player for YouTube Music's catalog, with genres, full albums and playlists. Windows, macOS, Linux, Android, iPhone and Docker. No account needed.

TypeScript

android tv: LEANBACK launcher, D-pad spatial navigation, 10-foot UI, TV build script

vahaa4a65e85913f
diff --git a/mobile/android/app/src/main/AndroidManifest.xml b/mobile/android/app/src/main/AndroidManifest.xml
index 3030edc..01bbd51 100644
--- a/mobile/android/app/src/main/AndroidManifest.xml
+++ b/mobile/android/app/src/main/AndroidManifest.xml
@@ -3,6 +3,10 @@
<!-- Permissions -->
+ <!-- Android TV: no touchscreen, leanback UI, network only -->
+ <uses-feature android:name="android.software.leanback" android:required="false" />
+ <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
+
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
@@ -11,6 +15,7 @@
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
+ android:banner="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
@@ -27,6 +32,8 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
+ <!-- Android TV launchers only show apps with this category -->
+ <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
diff --git a/tools/build-apk-tv.sh b/tools/build-apk-tv.sh
new file mode 100755
index 0000000..8196ea9
--- /dev/null
+++ b/tools/build-apk-tv.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+# Android TV build: web bundle in TV mode -> Capacitor sync -> debug APK -> install.
+set -euo pipefail
+cd "$(dirname "$0")/.."
+export JAVA_HOME="${JAVA_HOME:-/opt/homebrew/opt/openjdk@17}"
+export ANDROID_HOME="${ANDROID_HOME:-$HOME/Library/Android/sdk}"
+export PATH="$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$PATH"
+TV_HOST="${TV_HOST:-192.168.1.157:5555}"
+
+echo "== web bundle (TV mode) =="
+VITE_TV=1 pnpm --filter @musike/web build:native
+
+echo "== capacitor sync =="
+(cd mobile && npx cap sync android)
+
+echo "== gradle =="
+(cd mobile/android && ./gradlew assembleDebug --console=plain | tail -5)
+
+APK=mobile/android/app/build/outputs/apk/debug/app-debug.apk
+echo "== install to $TV_HOST =="
+adb connect "$TV_HOST" >/dev/null 2>&1 || true
+adb -s "$TV_HOST" install -r "$APK"
+adb -s "$TV_HOST" shell 'am force-stop app.musike.android; monkey -p app.musike.android -c android.intent.category.LEANBACK_LAUNCHER 1' >/dev/null 2>&1 || true
+echo "done: $APK"
diff --git a/web/src/hooks/useShortcuts.ts b/web/src/hooks/useShortcuts.ts
index e451266..af1cf9f 100644
--- a/web/src/hooks/useShortcuts.ts
+++ b/web/src/hooks/useShortcuts.ts
@@ -4,6 +4,7 @@ import { paths } from "../lib/routes";
import { selectCurrent, usePlayer } from "../player/player";
import { useLibrary } from "../stores/library";
import { useUi } from "../stores/ui";
+import { isTvMode } from "../tv/tvMode";
const isTyping = (el: EventTarget | null) =>
el instanceof HTMLElement && (el.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName));
@@ -16,6 +17,8 @@ export function useShortcuts() {
const onKey = (e: KeyboardEvent) => {
if (e.ctrlKey || e.metaKey || e.altKey || isTyping(e.target)) return;
if (useUi.getState().menu || useUi.getState().addToPlaylist) return;
+ // On Android TV the arrow keys belong to focus navigation, not to volume/seek.
+ if (isTvMode() && /^(ArrowUp|ArrowDown|ArrowLeft|ArrowRight| )$/.test(e.key)) return;
const p = usePlayer.getState();
const ui = useUi.getState();
const current = selectCurrent(p);
diff --git a/web/src/main.tsx b/web/src/main.tsx
index 05660b6..ed64db1 100644
--- a/web/src/main.tsx
+++ b/web/src/main.tsx
@@ -7,10 +7,15 @@ import { registerSW } from "virtual:pwa-register";
import App from "./App";
import { desktop } from "./lib/desktop";
import "./styles.css";
+import "./tv/tv.css";
+import { installTvMode } from "./tv/tvMode";
// The desktop app ships its files locally; a service worker would only risk serving an old version after updates.
if (!desktop) registerSW({ immediate: true });
+// Android TV: spatial D-pad navigation + 10-foot styling (no-op elsewhere).
+installTvMode();
+
createRoot(document.getElementById("root")!).render(
<StrictMode>
<BrowserRouter>
diff --git a/web/src/tv/tv.css b/web/src/tv/tv.css
new file mode 100644
index 0000000..5b42c43
--- /dev/null
+++ b/web/src/tv/tv.css
@@ -0,0 +1,47 @@
+/* Android TV (10-foot) tweaks. Loaded only when html[data-tv="1"] is set. */
+
+html[data-tv="1"] {
+ --tv-ring: #c6f26a;
+ font-size: 125%; /* everything a touch bigger for a 3 m viewing distance */
+}
+
+html[data-tv="1"] body {
+ overflow: hidden; /* no page scrolling: navigation moves focus */
+ cursor: none;
+}
+
+/* One clear focus ring on whatever the D-pad is standing on. */
+html[data-tv="1"] :focus {
+ outline: 4px solid var(--tv-ring) !important;
+ outline-offset: 3px !important;
+ border-radius: 12px;
+ z-index: 2;
+}
+
+html[data-tv="1"] :focus:not(:focus-visible) {
+ outline: 4px solid var(--tv-ring) !important;
+}
+
+/* Kill mouse-only affordances. */
+html[data-tv="1"] *:hover {
+ transform: none !important;
+}
+
+html[data-tv="1"] ::-webkit-scrollbar {
+ width: 6px;
+ height: 6px;
+}
+
+/* Bigger hit targets for lists and tiles. */
+html[data-tv="1"] button,
+html[data-tv="1"] [role="button"],
+html[data-tv="1"] a[href] {
+ min-width: 44px;
+ min-height: 44px;
+}
+
+/* Give rows more air so focus rings do not overlap. */
+html[data-tv="1"] ul,
+html[data-tv="1"] ol {
+ row-gap: 6px;
+}
diff --git a/web/src/tv/tvMode.ts b/web/src/tv/tvMode.ts
new file mode 100644
index 0000000..c80bdbf
--- /dev/null
+++ b/web/src/tv/tvMode.ts
@@ -0,0 +1,128 @@
+/**
+ * Android TV mode: 10-foot UI plus D-pad spatial navigation.
+ *
+ * Enabled by VITE_TV=1 at build time (see tools/build-apk-tv.sh) or by opening the app with ?tv=1,
+ * so the normal desktop/mobile builds are untouched.
+ */
+
+const FOCUSABLE = [
+ "button:not([disabled])",
+ "a[href]",
+ "input:not([disabled])",
+ "select:not([disabled])",
+ "textarea:not([disabled])",
+ '[tabindex]:not([tabindex="-1"])',
+ '[role="button"]',
+ '[role="slider"]',
+ "[data-tv-focusable]",
+].join(",");
+
+export function isTvMode(): boolean {
+ if (typeof window === "undefined") return false;
+ if (new URLSearchParams(window.location.search).has("tv")) return true;
+ if (import.meta.env.VITE_TV === "1") return true;
+ return document.documentElement.dataset.tv === "1";
+}
+
+const rectOf = (el: HTMLElement) => el.getBoundingClientRect();
+
+function visible(el: HTMLElement): boolean {
+ if (el === document.body) return false;
+ const r = rectOf(el);
+ if (r.width < 4 || r.height < 4) return false;
+ if (r.bottom < -40 || r.top > window.innerHeight + 40) return false;
+ const style = window.getComputedStyle(el);
+ if (style.visibility === "hidden" || style.opacity === "0") return false;
+ return true;
+}
+
+function candidates(): HTMLElement[] {
+ return Array.from(document.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(visible);
+}
+
+function center(el: HTMLElement) {
+ const r = rectOf(el);
+ return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
+}
+
+function move(direction: "left" | "right" | "up" | "down"): boolean {
+ const current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
+ const from = current && visible(current) ? center(current) : { x: window.innerWidth / 2, y: window.innerHeight / 2 };
+ let best: HTMLElement | null = null;
+ let bestScore = Number.POSITIVE_INFINITY;
+
+ for (const el of candidates()) {
+ if (el === current) continue;
+ const to = center(el);
+ const dx = to.x - from.x;
+ const dy = to.y - from.y;
+ const primary =
+ direction === "left" ? -dx : direction === "right" ? dx : direction === "up" ? -dy : dy;
+ if (primary < 6) continue; // not in that direction
+ const secondary = direction === "left" || direction === "right" ? Math.abs(dy) : Math.abs(dx);
+ const score = primary + secondary * 2.2; // stay in the same row/column when possible
+ if (score < bestScore) {
+ bestScore = score;
+ best = el;
+ }
+ }
+ if (!best) return false;
+ best.focus({ preventScroll: true });
+ best.scrollIntoView({ block: "nearest", inline: "nearest", behavior: "smooth" });
+ return true;
+}
+
+function activate(): boolean {
+ const el = document.activeElement;
+ if (!(el instanceof HTMLElement) || el === document.body) return false;
+ if (el instanceof HTMLInputElement && /^(text|search)$/.test(el.type)) return false;
+ el.click();
+ return true;
+}
+
+let installed = false;
+
+export function installTvMode() {
+ if (installed || !isTvMode()) return;
+ installed = true;
+ document.documentElement.dataset.tv = "1";
+
+ window.addEventListener(
+ "keydown",
+ (event) => {
+ const target = event.target as HTMLElement | null;
+ const typing =
+ target instanceof HTMLElement &&
+ (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName));
+ const key = event.key;
+ const handled = () => {
+ event.preventDefault();
+ event.stopPropagation();
+ };
+
+ if (key === "ArrowLeft" || key === "ArrowRight" || key === "ArrowUp" || key === "ArrowDown") {
+ if (typing) return;
+ if (move(key.replace("Arrow", "").toLowerCase() as "left" | "right" | "up" | "down")) handled();
+ return;
+ }
+ if ((key === "Enter" || key === " ") && !typing) {
+ // Buttons already activate themselves on Enter/Space.
+ if (document.activeElement instanceof HTMLButtonElement) return;
+ if (activate()) handled();
+ return;
+ }
+ if (key === "Escape" || key === "BrowserBack") {
+ // Let the app's own handlers/dialogs deal with Escape first.
+ }
+ },
+ { capture: true },
+ );
+
+ // First focus so the remote is immediately useful.
+ const focusFirst = () => {
+ const first = candidates()[0];
+ if (first && document.activeElement === document.body) first.focus({ preventScroll: true });
+ };
+ window.setTimeout(focusFirst, 400);
+ document.addEventListener("route-change", focusFirst as EventListener);
+}