import TrackPlayer, { isPlaying } from 'react-native-track-player'; import { setupPlayer } from './trackPlayer'; import { SAMPLE_TRACKS, toRntpTrack } from './sampleTracks'; /** * Transport actions screens call. Thin wrappers over RNTP so the UI never * imports the engine directly — at M3/M4 this is where a custom Media3 module * would slot in behind the same function signatures. */ /** * Set up the player and load the demo queue. Setup is deferred to here (a * user-initiated play) rather than app launch: RNTP starts a foreground * MediaSession service on setup, and Android only permits starting a foreground * service while the app is in the foreground. */ async function ensurePlayerReady(): Promise { await setupPlayer(); const queue = await TrackPlayer.getQueue(); if (queue.length === 0) { await TrackPlayer.add(SAMPLE_TRACKS.map(toRntpTrack)); } } /** M0 entry point: ensure the player is ready, then start playback. */ export async function playSample(): Promise { await ensurePlayerReady(); await TrackPlayer.play(); } export const play = (): Promise => TrackPlayer.play(); export const pause = (): Promise => TrackPlayer.pause(); export const seekTo = (seconds: number): Promise => TrackPlayer.seekTo(seconds); export async function togglePlay(): Promise { const { playing } = await isPlaying(); if (playing) { await TrackPlayer.pause(); } else { await ensurePlayerReady(); await TrackPlayer.play(); } } export async function skipToNext(): Promise { try { await TrackPlayer.skipToNext(); } catch { // no next track — ignore } } export async function skipToPrevious(): Promise { try { await TrackPlayer.skipToPrevious(); } catch { // no previous track — ignore } }