- Add native playlist windowing and lock-screen controls - Sync playback queues through a batched Convex song query --------- Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/17
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import type { AudioMetadata, AudioPlaylist } from "expo-audio"
|
|
|
|
/**
|
|
* Lock-screen options added in Expo SDK 58 (`expo-audio` playlist lock screen).
|
|
* SDK 57 only understands seek-forward/back; next/previous are ignored until upgrade.
|
|
*/
|
|
export type PlaylistLockScreenOptions = {
|
|
isLiveStream?: boolean
|
|
showNextTrack?: boolean
|
|
showPreviousTrack?: boolean
|
|
showSeekBackward?: boolean
|
|
showSeekForward?: boolean
|
|
}
|
|
|
|
type PlaylistLockScreenApi = {
|
|
setActiveForLockScreen: (
|
|
active: boolean,
|
|
metadata?: AudioMetadata,
|
|
options?: PlaylistLockScreenOptions
|
|
) => void
|
|
updateLockScreenMetadata?: (metadata: AudioMetadata) => void
|
|
clearLockScreenControls?: () => void
|
|
}
|
|
|
|
const TRACK_SKIP_LOCK_SCREEN_OPTIONS: PlaylistLockScreenOptions = {
|
|
showNextTrack: true,
|
|
showPreviousTrack: true,
|
|
showSeekForward: false,
|
|
showSeekBackward: false,
|
|
}
|
|
|
|
function lockScreenApi(playlist: AudioPlaylist): PlaylistLockScreenApi | null {
|
|
const candidate = playlist as AudioPlaylist & Partial<PlaylistLockScreenApi>
|
|
if (typeof candidate.setActiveForLockScreen !== "function") {
|
|
return null
|
|
}
|
|
return candidate as PlaylistLockScreenApi
|
|
}
|
|
|
|
export function activatePlaylistLockScreen(
|
|
playlist: AudioPlaylist,
|
|
metadata: AudioMetadata
|
|
) {
|
|
lockScreenApi(playlist)?.setActiveForLockScreen(
|
|
true,
|
|
metadata,
|
|
TRACK_SKIP_LOCK_SCREEN_OPTIONS
|
|
)
|
|
}
|
|
|
|
export function updatePlaylistLockScreenMetadata(
|
|
playlist: AudioPlaylist,
|
|
metadata: AudioMetadata
|
|
) {
|
|
const api = lockScreenApi(playlist)
|
|
if (typeof api?.updateLockScreenMetadata === "function") {
|
|
api.updateLockScreenMetadata(metadata)
|
|
return
|
|
}
|
|
api?.setActiveForLockScreen(true, metadata, TRACK_SKIP_LOCK_SCREEN_OPTIONS)
|
|
}
|
|
|
|
export function deactivatePlaylistLockScreen(playlist: AudioPlaylist) {
|
|
const api = lockScreenApi(playlist)
|
|
if (typeof api?.clearLockScreenControls === "function") {
|
|
api.clearLockScreenControls()
|
|
return
|
|
}
|
|
api?.setActiveForLockScreen(false)
|
|
}
|
|
|
|
export function releaseAudioPlaylist(playlist: AudioPlaylist) {
|
|
deactivatePlaylistLockScreen(playlist)
|
|
const withDestroy = playlist as AudioPlaylist & {
|
|
destroy?: () => void
|
|
}
|
|
if (typeof withDestroy.destroy === "function") {
|
|
withDestroy.destroy()
|
|
}
|
|
}
|