I am trying to embed a youtube iframe
into my application and listen to when that youtube video is loaded and ready to be played; my idea is to have a user press a play button, and not be shown the autoplaying embedded video until the video is completely ready to be played, and thus the user doesn't have to press a button and then wait for the embedded player to load.
I tried taking a look at this youtube iframe api documentation and various related stack overflows but still find myself stuck on how I can add a listener to my iframe player to find out when the video is completely loaded. The onLoad
global event attribute doesn't seem to be the solution I am looking for either. How can I find out when a particular youtube embedded iframe video is completely loaded and ready to be played? Am I implementing the iframe api incorrectly? You can see my attempt at it below. Thanks!
const YoutubePlayer: React = () => {
const readyFn = () => console.log('ready');
let player;
const onYouTubePlayerAPIReady = () => {
player = new window.YT.Player("youtube-iframe-id", {
height: '390',
width: '640',
videoId,
events: {
onReady: readyFn,
},
});
};
return (
<iframe
allowFullScreen
id="youtube-iframe-id"
src="https://www.youtube.com/embed/XmnZ9HZTHjw"
/>
);
};
ReactDOM.render(<YoutubePlayer />, app);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
<div id="app"></div>