Skip to content

Commit

Permalink
fix: properly end previous playing track
Browse files Browse the repository at this point in the history
  • Loading branch information
leinelissen committed Jun 18, 2023
1 parent f540424 commit 8ff785d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/utility/JellyfinApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions src/utility/PlaybackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down

0 comments on commit 8ff785d

Please sign in to comment.