File indexing completed on 2025-05-04 05:31:15

0001 import React from 'react';
0002 import { 
0003     Player, 
0004     ControlBar, 
0005     BigPlayButton,
0006     VolumeMenuButton, 
0007     LoadingSpinner,
0008     CurrentTimeDisplay,
0009     DurationDisplay } from 'video-react';
0010 
0011 class VideoPlayerWrapper extends React.Component {
0012     constructor(props, context){
0013         super(props, context);
0014 
0015         let hostLocation = window.location.href;
0016         if (!hostLocation.endsWith('/')) hostLocation += "/";
0017 
0018         this.state = {
0019             source:this.props.slide.url_preview,
0020             width:this.props.width,
0021             videoStarted:false,
0022             videoStopped:false,
0023             videoStartUrl:hostLocation + "startvideoajax?collection_id="+this.props.slide.collection_id+"&file_id="+this.props.slide.file_id,
0024             videoStopUrl:hostLocation + "stopvideoajax?media_view_id=",
0025             initialVolumeSet:false
0026         }
0027 
0028         this.onCinemaModeClick = this.onCinemaModeClick.bind(this);
0029         this.play = this.play.bind(this);
0030         this.pause = this.pause.bind(this);
0031     }
0032 
0033     componentDidMount() {
0034         this.refs.player.subscribeToStateChange(this.handleStateChange.bind(this));
0035     }
0036 
0037     handleStateChange(state, prevState) {
0038         this.setState({ player: state },function(){
0039             if (this.state.player){
0040                 if (this.state.initialVolumeSet === false){
0041                     const { player } = this.refs.player.getState();
0042                     this.refs.player.volume = 0.33;
0043                     this.setState({initialVolumeSet:true})
0044                 }
0045                 if (typeof this.state.player.videoWidth !== undefined) {
0046                     setTimeout(() => {
0047                         this.setState({videoRenderMask:false}) 
0048                     }, 1000);
0049                 }
0050                 if (this.state.player.hasStarted && this.state.videoStarted === false){
0051                     this.setState({videoStarted:true},function(){
0052                         const self = this;
0053                         $.ajax({url: this.state.videoStartUrl}).done(function(res) {
0054                             self.setState({videoStopUrl:self.state.videoStopUrl + res.MediaViewId})
0055                         });
0056                     });
0057                 } 
0058                 
0059                 if (this.state.player.paused && this.state.videoStarted === true && this.state.videoStopped === false){
0060                     this.setState({videoStopped:true},function(){
0061                         $.ajax({url: this.state.videoStopUrl}).done(function(res) {
0062                             // console.log(res)
0063                         });
0064                     });
0065                 }
0066 
0067                 if (state.isFullscreen === false && prevState.isFullscreen === true) this.props.onUpdateDimensions()
0068                 if (state.isFullscreen !== prevState.isFullscreen) this.props.onFullScreenToggle(state.isFullscreen)
0069 
0070                 
0071             }
0072         });
0073     }
0074 
0075     shouldComponentUpdate(nextProps, nextState){
0076         if (nextProps.playVideo === false) this.pause()
0077         return true;
0078     }
0079 
0080     onCinemaModeClick(){
0081         this.props.onCinemaModeClick()
0082     }
0083 
0084     play() {
0085         this.refs.player.play();
0086     }
0087     
0088     pause() {
0089         this.refs.player.pause();
0090     }
0091     
0092     render(){   
0093         let videoPlayerDisplay;
0094         if (this.state.source){
0095             let width = this.props.width;
0096             if (this.props.cinemaMode === false){
0097                 if (this.state.player){
0098                     const dimensionsRatio =  this.props.height / this.state.player.videoHeight;
0099                     if ((this.state.player.videoWidth * dimensionsRatio) < this.props.width) width = this.state.player.videoWidth * dimensionsRatio;
0100                 }
0101             }
0102             videoPlayerDisplay = (
0103                 <Player
0104                     ref="player"
0105                     fluid={false}
0106                     height={this.props.height}
0107                     width={width}
0108                     preload={"auto"}
0109                     src={this.state.source}>
0110                         <BigPlayButton position="center" />
0111                         <LoadingSpinner />
0112                         <ControlBar className="custom-video-player">
0113                             <VolumeMenuButton order={4.2} />
0114                             <CurrentTimeDisplay order={4.3} />
0115                             <DurationDisplay order={7.1} />
0116                             <a className="cinema-mode-button" onClick={this.onCinemaModeClick} order={7.3}><span></span></a>
0117                         </ControlBar>
0118                 </Player>            
0119             )
0120         }
0121         let videoRenderMask = <div className="video-render-mask"></div>
0122         if (this.state.videoRenderMask === false){ videoRenderMask = ''; }
0123         return (
0124             <div className={"react-player-container"}>
0125                 {videoRenderMask}
0126                 {videoPlayerDisplay}
0127             </div>
0128         )
0129     }
0130 }
0131 
0132 export default VideoPlayerWrapper;