Skip to content

Commit

Permalink
fix: actually send out /Playing events as session updates.
Browse files Browse the repository at this point in the history
This should more consistently result in output data in your play back reporting modules.

fixes #218
  • Loading branch information
leinelissen committed May 26, 2024
1 parent 823f7b5 commit b01470b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
22 changes: 11 additions & 11 deletions src/utility/JellyfinApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,22 @@ 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, trackIndex?: number) {
export async function sendPlaybackEvent(
path: string,
credentials: Credentials,
track?: Track
) {
// Extract all data from react-native-track-player
const [
currentTrack, position, repeatMode, volume, queue, state,
activeTrack, { position }, repeatMode, volume, { state },
] = await Promise.all([
TrackPlayer.getCurrentTrack(),
TrackPlayer.getPosition(),
track || TrackPlayer.getActiveTrack(),
TrackPlayer.getProgress(),
TrackPlayer.getRepeatMode(),
TrackPlayer.getVolume(),
TrackPlayer.getQueue(),
TrackPlayer.getState(),
TrackPlayer.getPlaybackState(),
]);

// 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 All @@ -333,8 +333,8 @@ export async function sendPlaybackEvent(path: string, credentials: Credentials,
PositionTicks: Math.round(position * 10_000_000),
PlaybackRate: 1,
PlayMethod: 'transcode',
MediaSourceId: track !== null ? queue[track].backendId : null,
ItemId: track !== null ? queue[track].backendId : null,
MediaSourceId: activeTrack?.backendId || null,
ItemId: activeTrack?.backendId || null,
CanSeek: true,
PlaybackStartTimeTicks: null,
};
Expand Down
22 changes: 12 additions & 10 deletions src/utility/PlaybackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ export default async function() {
TrackPlayer.seekTo(event.position);
});

TrackPlayer.addEventListener(Event.PlaybackTrackChanged, async (e) => {
TrackPlayer.addEventListener(Event.PlaybackActiveTrackChanged, 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 && 'track' in e) {
// GUARD: End the previous track if it's about to end
if ('nextTrack' in e && typeof e.track === 'number') {
sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin, e.track);
if (e.lastTrack) {
await sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin, e.lastTrack);
}

sendPlaybackEvent('/Sessions/Playing', settings.jellyfin);
await sendPlaybackEvent('/Sessions/Playing', settings.jellyfin, e.track);
}
});

Expand All @@ -69,14 +69,16 @@ export default async function() {
});

TrackPlayer.addEventListener(Event.PlaybackState, (event) => {
// GUARD: Only respond to stopped events
if (event.state === State.Stopped) {
// Retrieve the current settings from the Redux store
const settings = store.getState().settings;
// 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) {
// GUARD: Only report playback when the settings is enabled
if (settings.enablePlaybackReporting) {
// GUARD: Only respond to stopped events
if (event.state === State.Stopped) {
sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin);
} else if (event.state === State.Paused) {
sendPlaybackEvent('/Sessions/Playing/Progress', settings.jellyfin);
}
}
});
Expand Down

0 comments on commit b01470b

Please sign in to comment.