2020-06-16 17:51:51 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import TrackPlayer, { usePlaybackState, Track } from 'react-native-track-player';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This hook retrieves the current playing track from TrackPlayer
|
|
|
|
|
*/
|
|
|
|
|
export default function useCurrentTrack(): Track | undefined {
|
|
|
|
|
const state = usePlaybackState();
|
|
|
|
|
const [track, setTrack] = useState<Track>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchTrack = async () => {
|
|
|
|
|
const currentTrackId = await TrackPlayer.getCurrentTrack();
|
|
|
|
|
|
|
|
|
|
// GUARD: Only fetch current track if there is a current track
|
|
|
|
|
if (!currentTrackId) {
|
2020-08-28 12:45:00 +02:00
|
|
|
setTrack(undefined);
|
2020-06-16 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 23:34:35 +02:00
|
|
|
// GUARD: Only retrieve new track if it is different from the one we
|
|
|
|
|
// have currently in state.
|
|
|
|
|
if (currentTrackId === track?.id){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it is different, retrieve the track and save it
|
2021-02-11 23:55:49 +01:00
|
|
|
try {
|
|
|
|
|
const currentTrack = await TrackPlayer.getTrack(currentTrackId);
|
|
|
|
|
setTrack(currentTrack);
|
|
|
|
|
} catch {
|
|
|
|
|
// Due to the async nature, a track might be removed at the
|
|
|
|
|
// point when we try to retrieve it. If this happens, we'll just
|
|
|
|
|
// smother the error and wait for a new track update to
|
|
|
|
|
// finish.
|
|
|
|
|
}
|
2020-06-16 17:51:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchTrack();
|
2020-08-25 23:34:35 +02:00
|
|
|
}, [state, track, setTrack]);
|
2020-06-16 17:51:51 +02:00
|
|
|
|
|
|
|
|
return track;
|
|
|
|
|
}
|