-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4b340b
commit bc98574
Showing
6 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters