summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/components.fullscreen/fullscreen.video.js
blob: adbc22a852667eb923b290b93bd5a85a9a782bb8 (plain)
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
import React, { Component } from 'react'
import { connect } from 'react-redux'
import VimeoPlayer from 'app/utils/vendor/vimeo'

import actions from 'app/actions'
import { timestampToSeconds } from 'app/utils'
import { VideoScrubber } from '../components.media'

class FullscreenVideo extends Component {
  state = {
    // duration of the video, in seconds
    duration: 60.0,
    // percentage offset from vimeo. not used
    percent: 0.0,
    // current timestamp from vimeo, in seconds
    seconds: 0.0,
    // video start offset, in seconds
    video_start_ts: 0.0,
    // seek position, used to tell the player to seek
    seek: 0.0,
    // whether or not the scrubber is scrubbing
    scrubbing: false,
    // whether or not the video is ready and displaying an image
    ready: false,
  }
  constructor(props) {
    super(props)
    this.handlePlay = this.handlePlay.bind(this)
    this.handlePause = this.handlePause.bind(this)
    this.handleTimeUpdate = this.handleTimeUpdate.bind(this)
    this.handleScrub = this.handleScrub.bind(this)
    this.handleEnd = this.handleEnd.bind(this)
  }
  componentDidMount() {
    const { video_start_ts } = this.props.element.settings
    const seconds = timestampToSeconds(video_start_ts) || 0.0
    this.setState({
      video_start_ts: seconds,
      seek: seconds,
    })
  }
  componentDidUpdate(prevProps) {
    // if the play_ts jumped more than a second, seek
    const { play_ts, element } = this.props
    if (Math.abs(play_ts - prevProps.play_ts) > 1.0) {
      // handle seek
      const { duration, video_start_ts } = this.state
      const seek = ((play_ts - element.start_ts) % duration) + video_start_ts
      this.setState({ seek })
    }
    // if we started leaving the element...
    if (this.props.isLeaving && this.props.isLeaving !== prevProps.isLeaving && element.settings.unmuted) {
      this.fadeOutAudio()
    }
  }
  fadeOutAudio() {
    console.log("fade out audio", this.props.fadeOutDuration)
  }
  handlePlay() {
    this.setState({ ready: true })
  }
  handlePause() {

  }
  handleEnd() {

  }
  handleTimeUpdate(timing) {
    if (!this.state.scrubbing || ('scrubbing' in timing)) {
      this.setState(timing)
    }
  }
  handleScrub(timing) {
    console.log(timing)
    this.setState(timing)
  }

  render() {
    const { element, media, transitionDuration, play_ts, playing } = this.props
    const { duration, seconds, ready } = this.state
    const { color } = element
    const item = media.lookup[element.settings.media_id]
    const style = {
      backgroundColor: color.backgroundColor,
      color: color.textColor,
      transitionDuration,
    }
    const playerStyle = {
      opacity: ready ? 1.0 : 0.0
    }
    // 
    return (
      <div
        className='fullscreen-element video'
        style={style}
      >
        <div
          className='videoPlayer'
          style={playerStyle}
        >
          <VimeoPlayer
            video={item.url}
            paused={!playing}
            autoplay={true}
            autopause={false}
            muted={!element.settings.unmuted}
            loop={!!element.settings.loop}
            seek={this.state.seek}
            responsive={true}
            controls={false}
            byline={false}
            onPlay={this.handlePlay}
            onPause={this.handlePause}
            onTimeUpdate={this.handleTimeUpdate}
            onEnd={this.handleEnd}
          />
        </div>
        <VideoScrubber
          play_ts={play_ts}
          start_ts={element.start_ts}
          video_start_ts={this.state.video_start_ts}
          playing={playing}
          duration={element.duration}
          timing={this.state}
          onScrub={this.handleScrub}
        />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  viewer: state.viewer,
  play_ts: state.audio.play_ts,
  playing: state.audio.playing,
})

export default connect(mapStateToProps)(FullscreenVideo)