-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtt.js
60 lines (54 loc) · 1.29 KB
/
vtt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const STATE = {
STOP: 'stop',
PLAY: 'play'
}
export default class Vtt {
constructor (cueList) {
this.cues = []
for (let cue of cueList) {
this.cues.push(cue)
console.log(cue.startTime, cue.endTime)
}
this.state = STATE.STOP
this.activeCue = undefined
this.activeIndex = -1
this.startTimestamp = 0
}
getActiveCueIndex (dtSec, start) {
let nextCue = this.cues[start + 1]
if (nextCue && nextCue.startTime <= dtSec) {
return start + 1
}
return start
}
playFrame () {
const now = Date.now()
const dt = (now - this.startTimestamp) / 1000
if (this.activeIndex < this.cues.length-1) {
this.activeIndex = this.getActiveCueIndex(dt, this.activeIndex)
const newActiveCue = this.cues[this.activeIndex]
if (newActiveCue !== this.activeCue) {
console.log('newCue', newActiveCue)
this.activeCue = newActiveCue
if (this.oncuechange) {
this.oncuechange(this.activeCue)
}
}
} else {
this.state = STATE.STOP
}
}
play () {
requestAnimationFrame(() => {
this.playFrame()
if (this.state === STATE.PLAY) {
this.play()
}
})
}
start () {
this.state = STATE.PLAY
this.startTimestamp = Date.now()
this.play()
}
}