Skip to content

Instantly share code, notes, and snippets.

@scwood
Last active June 22, 2022 18:56
Show Gist options
  • Save scwood/322b0d1e4e1bf9701c736b05aa96e064 to your computer and use it in GitHub Desktop.
Save scwood/322b0d1e4e1bf9701c736b05aa96e064 to your computer and use it in GitHub Desktop.
React native track player pseudo code
// useInitializeTrackPlayer.ts
function useInitializeTrackPlayer() {
const [isInitialized, setIsInitialized] = useState(false);
useEffect(() => {
async function initializeTrackPlayer() {
await TrackPlayer.setupPlayer();
TrackPlayer.setOptions(...); // maybe await this? Not sure if it's async
setIsInitialized(true);
}
initializeTrackPlayer();
}, [])
return isInitialized;
}
// App.tsx
function App() {
const isTrackPlayerInitialized = useInitializeTrackPlayer();
if (!isTrackPlayerInitialized) {
// Render spinner animation, or just don't render anything at all,
// probably add a comment for the team on why you have to do this. Point
// to the ReactNativeTrackPlayer example project if you need to.
return ...
} else {
// Render your app as normal
return ...
}
}
// index.tsx
... // The rest of your index code
TrackPlayer.registerService(...);
// Nothing else TrackPlayer related here
// OtherComponent.tsx
function OtherComponent() {
return <button onClick={() => TrackPlayer.play()}>Play</button>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment