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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import React, { Component } from 'react'
import { connect } from 'react-redux'
import actions from 'app/actions'
import { VIDEO_SCRUBBER_HIDE_DELAY, VIDEO_SCRUBBER_HOVER_AREA } from 'app/constants'
import { clamp, timestamp } from 'app/utils'
import { PlayButton, VolumeControl, SubtitleButton } from 'app/views/viewer/nav/viewer.icons'
class VideoScrubber extends Component {
state = {
hovering: false,
showing: false,
}
constructor(props) {
super(props)
this.scrubberRef = React.createRef()
this.handleMouseDown = this.handleMouseDown.bind(this)
this.handleMouseMove = this.handleMouseMove.bind(this)
this.handleMouseUp = this.handleMouseUp.bind(this)
this.handleMouseEnter = this.handleMouseEnter.bind(this)
this.handleMouseLeave = this.handleMouseLeave.bind(this)
this.handleWindowFocus = this.handleWindowFocus.bind(this)
this.hide = this.hide.bind(this)
}
componentDidMount() {
window.addEventListener('mousemove', this.handleMouseMove)
window.addEventListener('mouseup', this.handleMouseUp)
window.addEventListener('focus', this.handleWindowFocus)
// this.show()
}
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove)
window.removeEventListener('mouseup', this.handleMouseUp)
window.removeEventListener('focus', this.handleWindowFocus)
clearTimeout(this.hideTimeout)
}
show() {
clearTimeout(this.hideTimeout)
this.hideTimeout = setTimeout(this.hide, VIDEO_SCRUBBER_HIDE_DELAY)
if (!this.showing) {
this.setState({ showing: true })
}
}
hide() {
clearTimeout(this.timeout)
this.setState({ showing: false })
this.defer = false
}
scrub(x, scrubbing) {
const { timing, start_ts, video_start_ts, onScrub, onToggle, duration, scrubMasterAudio } = this.props
const bounds = this.scrubberRef.current.getBoundingClientRect()
// get percent offset from the scrubber
const percent = clamp((x - bounds.left) / bounds.width, 0, 1)
// this offset in seconds based on the length of the fullscreen element
const seconds = percent * duration
// we can use this to seek the audio
if (scrubMasterAudio) {
actions.audio.seek(start_ts + seconds)
actions.audio.play()
}
// apply the video start offset.
// in case the video loops, modulo the length of the original video
const video_seek = ((seconds + video_start_ts) % timing.duration)
//
// console.log(start_ts, seconds)
onScrub({
seek: video_seek,
seconds: seconds,
scrubbing,
})
}
handleMouseDown(e) {
e.stopPropagation()
this.scrub(e.pageX, true)
}
handleMouseMove(e) {
e.stopPropagation()
if (e.pageY < window.innerHeight - VIDEO_SCRUBBER_HOVER_AREA) {
return
}
// console.log('move', this.defer)
if (this.defer) {
this.defer = false
this.show()
}
this.defer = true
clearTimeout(this.showTimeout)
this.showTimeout = setTimeout(() => {
this.defer = false
}, 16)
if (!this.props.timing.scrubbing) return
this.scrub(e.pageX, true)
}
handleMouseUp(e) {
e.stopPropagation()
this.props.onScrub({ scrubbing: false })
}
handleMouseEnter() {
this.setState({ hovering: true })
}
handleMouseLeave() {
this.setState({ hovering: false })
}
handleWindowFocus() {
this.setState({ showing: false, hovering: false })
clearTimeout(this.hideTimeout)
}
render() {
const { playing, start_ts, play_ts, volume, timing, video_start_ts, duration, cc, mediaItem, onToggle } = this.props
const { hovering, showing } = this.state
// remove video start offset from timing
// console.log(start_ts, play_ts)
const player_ts = play_ts - start_ts
// compute percent from the length of the fullscreen element
const percent = clamp(player_ts / duration, 0, 1)
// display timestamp from the fullscreen element too
const timestampText = timestamp(clamp(player_ts, 0, duration))
// show or hide the scrubber bar
const className = (hovering || showing) ? 'video-scrubber show' : 'video-scrubber'
// console.log(timing.seconds, video_start_ts, duration)
return (
<div className={className}>
<div className='start-controls'>
<PlayButton playing={playing} onClick={onToggle} />
</div>
<div
className='scrub-bar-container'
onMouseDown={this.handleMouseDown}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
ref={this.scrubberRef}
>
<div className='scrub-bar' />
<div
className='scrub-dot'
style={{ left: (100 * percent) + "%" }}
/>
<div
className='scrub-timestamp'
style={{ left: (100 * percent) + "%" }}
>
{timestampText}
</div>
</div>
<div className={mediaItem.settings.subtitles ? 'end-controls has-cc' : 'end-controls'}>
<div className='playerTime'>
{timestampText}
</div>
<VolumeControl volume={volume} />
{mediaItem.settings.subtitles && <SubtitleButton cc={cc} />}
</div>
</div>
)
}
}
const mapStateToProps = state => ({
volume: state.audio.volume,
cc: state.audio.cc,
})
export default connect(mapStateToProps)(VideoScrubber)
|