@zezosoft/zezo-ott-react-native-video-player
Version:
React Native OTT Video Player for Android & iOS. Supports playlists, seasons, auto-next playback, subtitles, theming, analytics, fullscreen mode, and custom controls. 🚀 Powered by ZezoSoft.
68 lines (67 loc) • 2.08 kB
JavaScript
;
import { useCallback } from 'react';
import { useVideoPlayerStore, mmkvStorage } from "../store/videoPlayerStore.js";
export const countWatchTime = (forceFlush = false) => {
const {
duration,
setStartWatchTime,
startWatchTime
} = useVideoPlayerStore.getState();
const currentTimeFromStorage = Number(mmkvStorage.getItem('currentTime') || 0);
const prevWatchTime = Number(mmkvStorage.getItem('current_watch_time') || 0);
const completed = duration > 0 && Math.round(currentTimeFromStorage / duration) >= 0.95;
let totalTimeWatched = prevWatchTime;
if (startWatchTime) {
const elapsedTime = (Date.now() - startWatchTime) / 1000;
totalTimeWatched = prevWatchTime + elapsedTime;
mmkvStorage.setItem('current_watch_time', totalTimeWatched.toString());
setStartWatchTime(null);
}
if (forceFlush) {
const latest = Number(mmkvStorage.getItem('current_watch_time') || '0');
return {
totalWatchTime: latest,
completed,
currentTime: currentTimeFromStorage
};
}
return {
totalWatchTime: totalTimeWatched,
completed,
currentTime: currentTimeFromStorage
};
};
export const useWatchReporter = ({
onWatchProgress
}) => {
const reportProgress = useCallback((event = 'PROGRESS', forcedCurrentTime) => {
const {
activeTrack,
duration
} = useVideoPlayerStore.getState();
if (!activeTrack) return;
const {
totalWatchTime,
completed,
currentTime
} = countWatchTime(event === 'VIDEO_CLOSE' || event === 'EPISODE_CHANGE' || event === 'COMPLETED');
const progress = {
contentId: activeTrack?.contentId || null,
episodeId: activeTrack?.episodeId || null,
contentType: activeTrack?.type,
currentTime: forcedCurrentTime ?? currentTime,
totalWatchTime,
duration: duration || 0,
isVideoCompleted: completed,
activeTrack
};
onWatchProgress?.({
...progress,
event
});
}, [onWatchProgress]);
return {
reportProgress
};
};
//# sourceMappingURL=useWatchReporter.js.map