精确有效观看视频

PCqiandao
harry 5 years ago
parent 762e340f80
commit a6d09fa89f

@ -1,5 +1,26 @@
import React, { useRef, useEffect, useCallback } from 'react'
Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
get: function () {
return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2)
}
})
function compareNumbers(a, b) {
return a - b;
}
function getTotalEffectTime(pos) {
pos.sort(compareNumbers)
let sum = 0
for (let i = 0; i < pos.length - 1; i++) {
let v = pos[i + 1] - pos[i]
if (v < 21) {
sum += v
}
}
return sum
}
const regex = /(android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini)/i
// https://www.showdoc.cc/educoder?page_id=4029884447803706
export default ({ src, videoId, logWatchHistory, courseId = null }) => {
@ -12,22 +33,21 @@ export default ({ src, videoId, logWatchHistory, courseId = null }) => {
const device = deviceMatch ? deviceMatch[0] : 'pc'
let totalDuration = 0
let totalTimePlayed = 0
let sumTimePlayed = 0
let lastUpdatedTime = 0
let lastEffectUpdatedTime = 0
let logId = null
let initLog = false
let timeTick = 20 // 20s
let logCount = 1
let isLoging = false
let isSeeking = false
let pos = []//
const log = useCallback((callback) => {
let params = {}
if (logId) {
params['log_id'] = logId
params['watch_duration'] = totalTimePlayed //
params['watch_duration'] = getTotalEffectTime(pos) //
params['total_duration'] = sumTimePlayed //
} else {
if (courseId) {
@ -82,6 +102,7 @@ export default ({ src, videoId, logWatchHistory, courseId = null }) => {
useEffect(() => {
function onPlay() {
pos.push(el.current.currentTime)
if (!initLog) {
initLog = true
log()
@ -92,13 +113,12 @@ export default ({ src, videoId, logWatchHistory, courseId = null }) => {
log(() => {
logId = null
logCount = 1
totalTimePlayed = 0
lastUpdatedTime = 0
sumTimePlayed = 0
initLog = false
isLoging = false
lastEffectUpdatedTime = 0
isSeeking = false
pos = []
})
}
@ -106,18 +126,16 @@ export default ({ src, videoId, logWatchHistory, courseId = null }) => {
if (!isSeeking) {
let newTime = el.current.currentTime
let timeDiff = newTime - lastUpdatedTime
let effectTimeDiff = newTime - lastEffectUpdatedTime
if (effectTimeDiff > 0) {
totalTimePlayed += effectTimeDiff
lastEffectUpdatedTime = newTime
}
sumTimePlayed += Math.abs(timeDiff)
lastUpdatedTime = newTime
if (!isLoging) {
if (sumTimePlayed - logCount * timeTick >= 0) {
logCount++
log()
//currenttime update before Seeking & Seeked fired
if (Math.abs(timeDiff) < 0.5) {
sumTimePlayed += Math.abs(timeDiff)
lastUpdatedTime = newTime
if (!isLoging) {
if (sumTimePlayed - logCount * timeTick >= 0) {
logCount++
pos.push(lastUpdatedTime)
log()
}
}
}
}
@ -126,11 +144,13 @@ export default ({ src, videoId, logWatchHistory, courseId = null }) => {
function onSeeking() {
isSeeking = true
lastUpdatedTime = el.current.currentTime
lastEffectUpdatedTime = el.current.currentTime
}
function onSeeked() {
if (el.current.playing) {
pos.push(el.current.currentTime, lastUpdatedTime)
}
lastUpdatedTime = el.current.currentTime
isSeeking = false
}

Loading…
Cancel
Save