fix: actually send out /Playing events as session updates.

This should more consistently result in output data in your play back reporting modules.

fixes #218
This commit is contained in:
Lei Nelissen
2024-05-26 18:00:05 +02:00
parent 823f7b59e8
commit b01470bde8
2 changed files with 23 additions and 21 deletions

View File

@@ -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,
@@ -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,
};

View File

@@ -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);
}
});
@@ -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);
}
}
});