2019-01-16 04:44:41 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-11-30 01:24:02 +08:00
|
|
|
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
|
|
|
|
import YouTube from 'react-youtube';
|
2019-01-16 04:44:41 +08:00
|
|
|
import { sendMessage, onMessage } from './service';
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:58:59 +08:00
|
|
|
const { PlayerState } = YouTube;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
class VideoPlayer extends Component {
|
2019-01-16 04:44:41 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.player = null;
|
|
|
|
this.syncInterval = null;
|
|
|
|
this.playerState = PlayerState.UNSTARTED;
|
|
|
|
this.presenterCommand = false;
|
|
|
|
this.preventStateChange = false;
|
|
|
|
this.opts = {
|
|
|
|
playerVars: {
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
autoplay: 1,
|
|
|
|
modestbranding: true,
|
|
|
|
rel: 0,
|
|
|
|
ecver: 2,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.keepSync = this.keepSync.bind(this);
|
|
|
|
this.handleResize = this.handleResize.bind(this);
|
|
|
|
this.handleOnReady = this.handleOnReady.bind(this);
|
|
|
|
this.handleStateChange = this.handleStateChange.bind(this);
|
|
|
|
this.resizeListener = () => {
|
|
|
|
setTimeout(this.handleResize, 0);
|
|
|
|
};
|
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:58:59 +08:00
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', this.resizeListener);
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
componentWillUpdate(nextProps) {
|
|
|
|
if (!nextProps.videoId) {
|
|
|
|
clearInterval(this.syncInterval);
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
2019-01-16 04:44:41 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-01-16 04:44:41 +08:00
|
|
|
window.removeEventListener('resize', this.resizeListener);
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
clearInterval(this.syncInterval);
|
|
|
|
this.player = null;
|
|
|
|
this.refPlayer = null;
|
|
|
|
}
|
2018-12-11 07:07:20 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
handleResize() {
|
|
|
|
if (!this.player || !this.refPlayer) {
|
2018-11-30 01:24:02 +08:00
|
|
|
return;
|
2019-01-16 04:44:41 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
const el = this.refPlayer;
|
|
|
|
const parent = el.parentElement;
|
|
|
|
const w = parent.clientWidth;
|
|
|
|
const h = parent.clientHeight;
|
|
|
|
const idealW = h * 16 / 9;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
if (idealW > w) {
|
2019-01-16 04:44:41 +08:00
|
|
|
this.player.setSize(w, w * 9 / 16);
|
2018-11-30 01:24:02 +08:00
|
|
|
} else {
|
|
|
|
this.player.setSize(idealW, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
keepSync() {
|
|
|
|
const { isPresenter } = this.props;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
if (isPresenter) {
|
|
|
|
this.syncInterval = setInterval(() => {
|
2019-01-16 04:44:41 +08:00
|
|
|
const curTime = this.player.getCurrentTime();
|
|
|
|
const rate = this.player.getPlaybackRate();
|
|
|
|
sendMessage('playerUpdate', { rate, time: curTime, state: this.playerState });
|
2018-11-30 01:24:02 +08:00
|
|
|
}, 2000);
|
|
|
|
} else {
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('play', ({ time }) => {
|
2018-11-30 01:24:02 +08:00
|
|
|
this.presenterCommand = true;
|
|
|
|
if (this.player) {
|
|
|
|
this.player.seekTo(time, true);
|
|
|
|
this.playerState = PlayerState.PLAYING;
|
|
|
|
this.player.playVideo();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('stop', ({ time }) => {
|
2018-11-30 01:24:02 +08:00
|
|
|
this.presenterCommand = true;
|
|
|
|
|
|
|
|
if (this.player) {
|
|
|
|
this.playerState = PlayerState.PAUSED;
|
|
|
|
this.player.seekTo(time, true);
|
|
|
|
this.player.pauseVideo();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('playerUpdate', (data) => {
|
2018-11-30 01:24:02 +08:00
|
|
|
if (!this.player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
if (data.rate !== this.player.getPlaybackRate()) {
|
2018-11-30 01:24:02 +08:00
|
|
|
this.player.setPlaybackRate(data.rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(this.player.getCurrentTime() - data.time) > 2) {
|
|
|
|
this.player.seekTo(data.time, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.playerState !== data.state) {
|
|
|
|
this.presenterCommand = true;
|
|
|
|
this.playerState = data.state;
|
|
|
|
if (this.playerState === PlayerState.PLAYING) {
|
|
|
|
this.player.playVideo();
|
|
|
|
} else {
|
|
|
|
this.player.pauseVideo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('changePlaybackRate', (rate) => {
|
2018-11-30 01:24:02 +08:00
|
|
|
this.player.setPlaybackRate(rate);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
handleOnReady(event) {
|
|
|
|
const { isPresenter } = this.props;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
this.player = event.target;
|
|
|
|
this.player.pauseVideo();
|
|
|
|
|
|
|
|
this.keepSync();
|
|
|
|
|
|
|
|
if (!isPresenter) {
|
2019-01-16 04:44:41 +08:00
|
|
|
sendMessage('viewerJoined');
|
2018-11-30 01:24:02 +08:00
|
|
|
} else {
|
|
|
|
this.player.playVideo();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleResize();
|
2019-01-16 04:44:41 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
handleStateChange(event) {
|
|
|
|
const { isPresenter } = this.props;
|
|
|
|
const curTime = this.player.getCurrentTime();
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
if (this.preventStateChange && [PlayerState.PLAYING, PlayerState.PAUSED].includes(event.data)) {
|
|
|
|
this.preventStateChange = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.playerState === event.data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.data === PlayerState.PLAYING) {
|
|
|
|
if (isPresenter) {
|
2019-01-16 04:44:41 +08:00
|
|
|
sendMessage('play', { time: curTime });
|
2018-11-30 01:24:02 +08:00
|
|
|
this.playerState = event.data;
|
|
|
|
} else if (!this.presenterCommand) {
|
|
|
|
this.player.seekTo(curTime, true);
|
|
|
|
this.preventStateChange = true;
|
|
|
|
this.player.pauseVideo();
|
|
|
|
} else {
|
|
|
|
this.playerState = event.data;
|
|
|
|
this.presenterCommand = false;
|
|
|
|
}
|
|
|
|
} else if (event.data === PlayerState.PAUSED) {
|
|
|
|
if (isPresenter) {
|
2019-01-16 04:44:41 +08:00
|
|
|
sendMessage('stop', { time: curTime });
|
2018-11-30 01:24:02 +08:00
|
|
|
this.playerState = event.data;
|
|
|
|
} else if (!this.presenterCommand) {
|
|
|
|
this.player.seekTo(curTime);
|
|
|
|
this.preventStateChange = true;
|
|
|
|
this.player.playVideo();
|
|
|
|
} else {
|
|
|
|
this.playerState = event.data;
|
|
|
|
this.presenterCommand = false;
|
|
|
|
}
|
|
|
|
}
|
2018-12-11 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
render () {
|
2019-01-16 04:44:41 +08:00
|
|
|
const { videoId } = this.props;
|
|
|
|
const { opts, handleOnReady, handleStateChange } = this;
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
id="youtube-video-player"
|
|
|
|
data-test="videoPlayer"
|
2019-01-16 04:44:41 +08:00
|
|
|
ref={(ref) => { this.refPlayer = ref; }}
|
2018-11-30 01:24:02 +08:00
|
|
|
>
|
|
|
|
<YouTube
|
2019-01-16 04:44:41 +08:00
|
|
|
videoId={videoId}
|
|
|
|
opts={opts}
|
|
|
|
onReady={handleOnReady}
|
|
|
|
onStateChange={handleStateChange}
|
|
|
|
/>
|
2018-11-30 01:24:02 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
export default injectWbResizeEvent(VideoPlayer);
|