improve web playback log behavior

This commit is contained in:
2022-11-04 23:50:38 +08:00
parent d478923ce0
commit 061ef9bdc9

View File

@@ -24,7 +24,13 @@ function AudioPlayer(props) {
const [beginPlayTime, setBeginPlayTime] = useState(null); const [beginPlayTime, setBeginPlayTime] = useState(null);
const [lastID, setLastID] = useState(null); const [lastID, setLastID] = useState(null);
const recordPlaybackHistory = async (file_id, method, duration) => { const recordPlaybackHistory = async (file_id, method) => {
if (file_id === null) {
return
}
const endPlayTime = new Date()
const duration = parseInt((endPlayTime - beginPlayTime) / 1000)
setBeginPlayTime(endPlayTime)
await fetch('/api/v1/record_playback', { await fetch('/api/v1/record_playback', {
method: "POST", method: "POST",
headers: { headers: {
@@ -48,27 +54,10 @@ function AudioPlayer(props) {
}); });
}, []); }, []);
useEffect(() => { const updatePlayMode = () => {
// media session related staff
navigator.mediaSession.metadata = new window.MediaMetadata({
title: props.playingFile.filename,
album: props.playingFile.foldername,
artwork: [{src: "/favicon.png", type: "image/png"}],
});
// no playing file
if (props.playingFile.id === undefined) { if (props.playingFile.id === undefined) {
setPlayingURL(""); return
return;
} }
// crrently playing file, record interupt
if (playingURL) {
const endPlayTime = new Date()
const duration = parseInt((endPlayTime - beginPlayTime) / 1000)
recordPlaybackHistory(lastID, 2, duration)
}
setLastID(props.playingFile.id)
// have playingFile, record begin time
setBeginPlayTime(new Date())
if (raw) { if (raw) {
console.log("Play raw file"); console.log("Play raw file");
setPlayingURL("/api/v1/get_file_direct?id=" + props.playingFile.id); setPlayingURL("/api/v1/get_file_direct?id=" + props.playingFile.id);
@@ -103,7 +92,37 @@ function AudioPlayer(props) {
); );
} }
} }
}, [props.playingFile.id, raw, prepare, selectedFfmpegConfig]); }
useEffect(() => {
// media session related staff
navigator.mediaSession.metadata = new window.MediaMetadata({
title: props.playingFile.filename,
album: props.playingFile.foldername,
artwork: [{src: "/favicon.png", type: "image/png"}],
});
// no playing file
if (props.playingFile.id === undefined) {
setPlayingURL("");
// 3 music stopped
recordPlaybackHistory(lastID, 3)
return;
}
// crrently playing file, record interupt
if (playingURL) {
// 2 music changed
recordPlaybackHistory(lastID, 2)
}
setLastID(props.playingFile.id)
// have playingFile, record begin time
setBeginPlayTime(new Date())
updatePlayMode()
}, [props.playingFile.id]);
useEffect(() => {
updatePlayMode()
}, [raw, prepare, selectedFfmpegConfig])
let navigate = useNavigate(); let navigate = useNavigate();
@@ -145,10 +164,6 @@ function AudioPlayer(props) {
<button <button
onClick={() => { onClick={() => {
props.setPlayingFile({}); props.setPlayingFile({});
const endPlayTime = new Date()
const duration = parseInt((endPlayTime - beginPlayTime) / 1000)
setBeginPlayTime(endPlayTime)
recordPlaybackHistory(props.playingFile.id, 3, duration)
}} }}
> >
{Tr("Stop")} {Tr("Stop")}
@@ -230,10 +245,15 @@ function AudioPlayer(props) {
if (loop) { if (loop) {
player.play() player.play()
} }
const endPlayTime = new Date() // 1 music finished
const duration = parseInt((endPlayTime - beginPlayTime) / 1000) recordPlaybackHistory(props.playingFile.id, 1)
setBeginPlayTime(endPlayTime) }}
recordPlaybackHistory(props.playingFile.id, 1, duration) onPause={() => {
// 4 music paused
recordPlaybackHistory(props.playingFile.id, 4)
}}
onPlay={() => {
setBeginPlayTime(new Date());
}} }}
></audio> ></audio>