fix: properly end previous playing track

This commit is contained in:
Lei Nelissen
2023-06-18 22:04:20 +02:00
parent f540424edd
commit 8ff785da40
2 changed files with 12 additions and 4 deletions

View File

@@ -261,10 +261,10 @@ const RepeatModeMap: Record<RepeatMode, string> = {
* This will generate the payload that is required for playback events and send
* it to the supplied path.
*/
export async function sendPlaybackEvent(path: string, credentials: Credentials) {
export async function sendPlaybackEvent(path: string, credentials: Credentials, trackIndex?: number) {
// Extract all data from react-native-track-player
const [
track, position, repeatMode, volume, queue, state,
currentTrack, position, repeatMode, volume, queue, state,
] = await Promise.all([
TrackPlayer.getCurrentTrack(),
TrackPlayer.getPosition(),
@@ -274,6 +274,9 @@ export async function sendPlaybackEvent(path: string, credentials: Credentials)
TrackPlayer.getState(),
]);
// Switch between overriden track index and current track
const track = trackIndex !== undefined ? trackIndex : currentTrack;
// Generate a payload from the gathered data
const payload = {
VolumeLevel: volume * 100,

View File

@@ -36,12 +36,17 @@ export default async function() {
TrackPlayer.seekTo(event.position);
});
TrackPlayer.addEventListener(Event.PlaybackTrackChanged, () => {
TrackPlayer.addEventListener(Event.PlaybackTrackChanged, async (e) => {
// Retrieve the current settings from the Redux store
const settings = store.getState().settings;
// GUARD: Only report playback when the settings is enabled
if (settings.enablePlaybackReporting) {
if (settings.enablePlaybackReporting && 'track' in e) {
// GUARD: End the previous track if it's about to end
if ('nextTrack' in e && typeof e.track === 'number') {
sendPlaybackEvent('/Sessions/Stopped', settings.jellyfin, e.track);
}
sendPlaybackEvent('/Sessions/Playing', settings.jellyfin);
}
});