Skip to content

Commit

Permalink
add RNTP setup
Browse files Browse the repository at this point in the history
  • Loading branch information
andordavoti committed Mar 28, 2024
1 parent d4b340b commit bc98574
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 6 deletions.
21 changes: 16 additions & 5 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import TrackPlayer from "react-native-track-player";
import { playbackService } from "./utils/trackPlayer";
import { useSetupPlayer } from "./hooks/useSetupPlayer";

TrackPlayer.registerPlaybackService(() => playbackService);

export default function App() {
const isPlayerReady = useSetupPlayer();

if (!isPlayerReady) {
return <Text>Loading...</Text>;
}

return (
<View style={styles.container}>
<Text>Open up App.tsx to start working on your app!</Text>
Expand All @@ -13,8 +24,8 @@ export default function App() {
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
29 changes: 29 additions & 0 deletions hooks/useSetupPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import TrackPlayer, { Track } from "react-native-track-player";
import { useEffect, useState } from "react";
import { setupPlaybackService } from "../utils/trackPlayer";
import playlistData from "../lib/playlist.json";

export const useSetupPlayer = () => {
const [playerReady, setPlayerReady] = useState<boolean>(false);

useEffect(() => {
let unmounted = false;

(async () => {
await setupPlaybackService();
if (unmounted) return;
setPlayerReady(true);
const queue = await TrackPlayer.getQueue();
if (unmounted) return;
if (queue.length <= 0) {
await TrackPlayer.add(playlistData as Track[]);
}
})();

return () => {
unmounted = true;
};
}, []);

return playerReady;
};
52 changes: 52 additions & 0 deletions lib/playlist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"url": "https://rntp.dev/example/Longing.mp3",
"title": "Longing",
"artist": "David Chavez",
"artwork": "https://rntp.dev/example/Longing.jpeg",
"duration": 143
},
{
"url": "https://rntp.dev/example/Soul%20Searching.mp3",
"title": "Soul Searching (Demo)",
"artist": "David Chavez",
"artwork": "https://rntp.dev/example/Soul%20Searching.jpeg",
"duration": 77
},
{
"url": "https://rntp.dev/example/Lullaby%20(Demo).mp3",
"title": "Lullaby (Demo)",
"artist": "David Chavez",
"artwork": "https://rntp.dev/example/Lullaby%20(Demo).jpeg",
"duration": 71
},
{
"url": "https://rntp.dev/example/Rhythm%20City%20(Demo).mp3",
"title": "Rhythm City (Demo)",
"artist": "David Chavez",
"artwork": "https://rntp.dev/example/Rhythm%20City%20(Demo).jpeg",
"duration": 106
},
{
"url": "https://rntp.dev/example/hls/whip/playlist.m3u8",
"title": "Whip",
"artist": "prazkhanal",
"artwork": "https://rntp.dev/example/hls/whip/whip.jpeg",
"type": "hls"
},
{
"url": "https://ais-sa5.cdnstream1.com/b75154_128mp3",
"title": "Smooth Jazz 24/7",
"artist": "New York, NY",
"artwork": "https://rntp.dev/example/smooth-jazz-24-7.jpeg",
"isLiveStream": true
},
{
"url": "https://traffic.libsyn.com/atpfm/atp545.mp3",
"title": "Chapters"
},
{
"url": "https://kut.streamguys1.com/kutx-app.aac?listenerId=123456784123",
"title": "KUTX"
}
]
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.73.6",
"react-native-web": "~0.19.6"
"react-native-track-player": "^4.1.1",
"react-native-web": "~0.19.6",
"shaka-player": "^4.7.11"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
71 changes: 71 additions & 0 deletions utils/trackPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import TrackPlayer, {
Event,
Capability,
AppKilledPlaybackBehavior,
} from "react-native-track-player";

const { Play, Pause, Stop, SeekTo, SkipToNext, SkipToPrevious } = Capability;

const setupPlayer = async (
options: Parameters<typeof TrackPlayer.setupPlayer>[0]
) => {
const setup = async () => {
try {
await TrackPlayer.setupPlayer(options);
console.log("Player setup successful");
} catch (error) {
console.log("🚀 ~ setup ~ error:", error);
return (error as Error & { code?: string }).code;
}
};
while ((await setup()) === "android_cannot_setup_player_in_background") {
// A timeout will mostly only execute when the app is in the foreground,
// and even if we were in the background still, it will reject the promise
// and we'll try again:
await new Promise<void>((resolve) => setTimeout(resolve, 1));
}
};

export const setupPlaybackService = async () => {
await setupPlayer({ autoHandleInterruptions: true });

await TrackPlayer.updateOptions({
android: {
appKilledPlaybackBehavior:
AppKilledPlaybackBehavior.StopPlaybackAndRemoveNotification,
},
// This flag is now deprecated. Please use the above to define playback mode.
// stoppingAppPausesPlayback: true,
capabilities: [Play, Pause, Stop, SeekTo, SkipToNext, SkipToPrevious],
compactCapabilities: [Play, Pause, SeekTo, SkipToNext, SkipToPrevious],
progressUpdateEventInterval: 2,
});
};

export const playbackService = async () => {
TrackPlayer.addEventListener(Event.RemotePlay, () => {
TrackPlayer.play();
});

TrackPlayer.addEventListener(Event.RemotePause, () => {
TrackPlayer.pause();
});

TrackPlayer.addEventListener(Event.RemoteNext, () => {
TrackPlayer.skipToNext();
});

TrackPlayer.addEventListener(Event.RemotePrevious, async () => {
const { position } = await TrackPlayer.getProgress();

if (position > 3) {
TrackPlayer.seekTo(0);
} else {
TrackPlayer.skipToPrevious();
}
});

TrackPlayer.addEventListener(Event.RemoteSeek, ({ position }) => {
TrackPlayer.seekTo(position);
});
};
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,11 @@ electron-to-chromium@^1.4.668:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.719.tgz#22a94ce7a5150511ba88e900836039e159efe22c"
integrity sha512-FbWy2Q2YgdFzkFUW/W5jBjE9dj+804+98E4Pup78JBPnbdb3pv6IneY2JCPKdeKLh3AOKHQeYf+KwLr7mxGh6Q==

eme-encryption-scheme-polyfill@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/eme-encryption-scheme-polyfill/-/eme-encryption-scheme-polyfill-2.1.1.tgz#91c823ed584e8ec5a9f03a6a676def8f80c57a4c"
integrity sha512-njD17wcUrbqCj0ArpLu5zWXtaiupHb/2fIUQGdInf83GlI+Q6mmqaPGLdrke4savKAu15J/z1Tg/ivDgl14g0g==

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
Expand Down Expand Up @@ -5195,6 +5200,11 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-native-track-player@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/react-native-track-player/-/react-native-track-player-4.1.1.tgz#96f8688f270c5a01bc597b6331843b191402502b"
integrity sha512-E5N/eK/+HtAVJUAzXpm1cWz8ROheV9jb0TI6h2bM+333U+DWibTTnT2T1122FkCoXLXIYavtm2FR2if+5jH8cA==

react-native-web@~0.19.6:
version "0.19.10"
resolved "https://registry.yarnpkg.com/react-native-web/-/react-native-web-0.19.10.tgz#5f7205f8909c0889bc89c9fde7c6e287defa7c63"
Expand Down Expand Up @@ -5586,6 +5596,13 @@ setprototypeof@1.2.0:
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.2.0.tgz#66c9a24a73f9fc28cbe66b09fed3d33dcaf1b424"
integrity sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==

shaka-player@^4.7.11:
version "4.7.11"
resolved "https://registry.yarnpkg.com/shaka-player/-/shaka-player-4.7.11.tgz#962d2a205b86ff3aecb20949f1738c36b0eb98de"
integrity sha512-zHyuL/cph3xKbci/Ovba/Swyni6g3cj79cN+84BCCrD3tWR9WEBwc8svcqPIXNQX/7VYb3uwaOa+85eBQKPkMg==
dependencies:
eme-encryption-scheme-polyfill "^2.1.1"

shallow-clone@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/shallow-clone/-/shallow-clone-3.0.1.tgz#8f2981ad92531f55035b01fb230769a40e02efa3"
Expand Down

0 comments on commit bc98574

Please sign in to comment.