Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file named Player.js #27

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/components/Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// use the command "npm install react-player --save" to install react player if not working

//-------------------------------------------------------------------------//


//This code should be used in App.js as shown below where Player.js should
//be in in "components" folder inside src

//import React from "react";
// import Player from "./components/Player.js";

// const App = () => {
// return (

// <div >
//use paragraph to keep some distance
// { <p>
// lorem ipsum
// </p> }
// <Player />
// </div>
// );
// };


// export default App;


//------------------------------------------------------------------------//

//This should be written in "index.css" file to place the video in center

// .player-container {
// height: 100%;
// display: flex;
// justify-content: center;
// }

//------------------------------------------------------------------------//


import React from "react";
import ReactPlayer from "react-player";


class Player extends React.Component {
constructor(props) {
super(props);
this.player = React.createRef();
this.state = { inView: false }; //if the video has to be placed initially it has to be kept at
//some distance from marhin for code to work
}
componentDidMount = () => {
window.addEventListener("scroll", this.handleScroll);
};

componentWillUnmount = () => {
window.removeEventListener("scroll", this.handleScroll);
};

handleScroll = e => {
const element = this.player.current;
const bounding = element.getBoundingClientRect();

const inView =
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <=
(window.innerWidth || document.documentElement.clientWidth);

this.setState(state => {
if (state.inView !== inView) {
console.log("Player is:", inView);
return { inView: inView };
}
});
};
//place the video url here
link_taken = "https://www.youtube.com/watch?v=V1Pl8CzNzCw";

render() {
return (
<div className="player-container" ref={this.player}>
<ReactPlayer
//The video link holder should be placed here
url={this.link_taken}
playing={this.state.inView}
/>
</div>
);
}
}

export default Player;