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:
@@ -307,22 +307,22 @@ const RepeatModeMap: Record<RepeatMode, string> = {
|
|||||||
* This will generate the payload that is required for playback events and send
|
* This will generate the payload that is required for playback events and send
|
||||||
* it to the supplied path.
|
* 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
|
// Extract all data from react-native-track-player
|
||||||
const [
|
const [
|
||||||
currentTrack, position, repeatMode, volume, queue, state,
|
activeTrack, { position }, repeatMode, volume, { state },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
TrackPlayer.getCurrentTrack(),
|
track || TrackPlayer.getActiveTrack(),
|
||||||
TrackPlayer.getPosition(),
|
TrackPlayer.getProgress(),
|
||||||
TrackPlayer.getRepeatMode(),
|
TrackPlayer.getRepeatMode(),
|
||||||
TrackPlayer.getVolume(),
|
TrackPlayer.getVolume(),
|
||||||
TrackPlayer.getQueue(),
|
TrackPlayer.getPlaybackState(),
|
||||||
TrackPlayer.getState(),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Switch between overriden track index and current track
|
|
||||||
const track = trackIndex !== undefined ? trackIndex : currentTrack;
|
|
||||||
|
|
||||||
// Generate a payload from the gathered data
|
// Generate a payload from the gathered data
|
||||||
const payload = {
|
const payload = {
|
||||||
VolumeLevel: volume * 100,
|
VolumeLevel: volume * 100,
|
||||||
@@ -333,8 +333,8 @@ export async function sendPlaybackEvent(path: string, credentials: Credentials,
|
|||||||
PositionTicks: Math.round(position * 10_000_000),
|
PositionTicks: Math.round(position * 10_000_000),
|
||||||
PlaybackRate: 1,
|
PlaybackRate: 1,
|
||||||
PlayMethod: 'transcode',
|
PlayMethod: 'transcode',
|
||||||
MediaSourceId: track !== null ? queue[track].backendId : null,
|
MediaSourceId: activeTrack?.backendId || null,
|
||||||
ItemId: track !== null ? queue[track].backendId : null,
|
ItemId: activeTrack?.backendId || null,
|
||||||
CanSeek: true,
|
CanSeek: true,
|
||||||
PlaybackStartTimeTicks: null,
|
PlaybackStartTimeTicks: null,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,18 +37,18 @@ export default async function() {
|
|||||||
TrackPlayer.seekTo(event.position);
|
TrackPlayer.seekTo(event.position);
|
||||||
});
|
});
|
||||||
|
|
||||||
TrackPlayer.addEventListener(Event.PlaybackTrackChanged, async (e) => {
|
TrackPlayer.addEventListener(Event.PlaybackActiveTrackChanged, async (e) => {
|
||||||
// Retrieve the current settings from the Redux store
|
// Retrieve the current settings from the Redux store
|
||||||
const settings = store.getState().settings;
|
const settings = store.getState().settings;
|
||||||
|
|
||||||
// GUARD: Only report playback when the settings is enabled
|
// GUARD: Only report playback when the settings is enabled
|
||||||
if (settings.enablePlaybackReporting && 'track' in e) {
|
if (settings.enablePlaybackReporting && 'track' in e) {
|
||||||
// GUARD: End the previous track if it's about to end
|
// GUARD: End the previous track if it's about to end
|
||||||
if ('nextTrack' in e && typeof e.track === 'number') {
|
if (e.lastTrack) {
|
||||||
sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin, e.track);
|
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) => {
|
TrackPlayer.addEventListener(Event.PlaybackState, (event) => {
|
||||||
// GUARD: Only respond to stopped events
|
|
||||||
if (event.state === State.Stopped) {
|
|
||||||
// Retrieve the current settings from the Redux store
|
// Retrieve the current settings from the Redux store
|
||||||
const settings = store.getState().settings;
|
const settings = store.getState().settings;
|
||||||
|
|
||||||
// GUARD: Only report playback when the settings is enabled
|
// GUARD: Only report playback when the settings is enabled
|
||||||
if (settings.enablePlaybackReporting) {
|
if (settings.enablePlaybackReporting) {
|
||||||
|
// GUARD: Only respond to stopped events
|
||||||
|
if (event.state === State.Stopped) {
|
||||||
sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin);
|
sendPlaybackEvent('/Sessions/Playing/Stopped', settings.jellyfin);
|
||||||
|
} else if (event.state === State.Paused) {
|
||||||
|
sendPlaybackEvent('/Sessions/Playing/Progress', settings.jellyfin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user