fix: send last position for Stopped event

instead of the current position, which is `0` when we move to a new track
This commit is contained in:
Lei Nelissen
2024-07-21 22:10:10 +02:00
parent 746c96d459
commit 68c8808188
2 changed files with 5 additions and 4 deletions

View File

@@ -17,11 +17,12 @@ const RepeatModeMap: Record<RepeatMode, string> = {
*/ */
export async function sendPlaybackEvent( export async function sendPlaybackEvent(
path: string, path: string,
track?: Track track?: Track,
lastPosition?: number,
) { ) {
// Extract all data from react-native-track-player // Extract all data from react-native-track-player
const [ const [
activeTrack, { position }, repeatMode, volume, { state }, activeTrack, { position: currentPosition }, repeatMode, volume, { state },
] = await Promise.all([ ] = await Promise.all([
track || TrackPlayer.getActiveTrack(), track || TrackPlayer.getActiveTrack(),
TrackPlayer.getProgress(), TrackPlayer.getProgress(),
@@ -37,7 +38,7 @@ export async function sendPlaybackEvent(
IsPaused: state === State.Paused, IsPaused: state === State.Paused,
RepeatMode: RepeatModeMap[repeatMode], RepeatMode: RepeatModeMap[repeatMode],
ShuffleMode: 'Sorted', ShuffleMode: 'Sorted',
PositionTicks: Math.round(position * 10_000_000), PositionTicks: Math.round((lastPosition || currentPosition) * 10_000_000),
PlaybackRate: 1, PlaybackRate: 1,
PlayMethod: 'transcode', PlayMethod: 'transcode',
MediaSourceId: activeTrack?.backendId || null, MediaSourceId: activeTrack?.backendId || null,

View File

@@ -45,7 +45,7 @@ export default async function() {
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 (e.lastTrack) { if (e.lastTrack) {
await sendPlaybackEvent('/Sessions/Playing/Stopped', e.lastTrack); await sendPlaybackEvent('/Sessions/Playing/Stopped', e.lastTrack, e.lastPosition);
} }
await sendPlaybackEvent('/Sessions/Playing', e.track); await sendPlaybackEvent('/Sessions/Playing', e.track);