How to integrate Youtube Iframe api in reactjs solution

前端 未结 1 1239
梦谈多话
梦谈多话 2021-01-12 16:52

In react, I am trying to create a component for custom youtube player, so that I can introduce a new player controls bar. Form youtube iframe API, it was mentioned to use fo

相关标签:
1条回答
  • 2021-01-12 17:27

    Here is a YouTubeVideo React component I wrote for a project recently.

    When the component mounts, it checks to see if the YouTube iFrame API has already been loaded.

    • If so, then it calls the API to create a new YouTube Player object directly.
    • If not, it first waits for the script to load asynchronously and then load the video.

    import PropTypes from 'prop-types';
    import React from 'react';
    
    import classes from 'styles/YouTubeVideo.module.css';
    
    class YouTubeVideo extends React.PureComponent {
      static propTypes = {
        id: PropTypes.string.isRequired,
      };
    
      componentDidMount = () => {
        // On mount, check to see if the API script is already loaded
    
        if (!window.YT) { // If not, load the script asynchronously
          const tag = document.createElement('script');
          tag.src = 'https://www.youtube.com/iframe_api';
    
          // onYouTubeIframeAPIReady will load the video after the script is loaded
          window.onYouTubeIframeAPIReady = this.loadVideo;
    
          const firstScriptTag = document.getElementsByTagName('script')[0];
          firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
        } else { // If script is already there, load the video directly
          this.loadVideo();
        }
      };
    
      loadVideo = () => {
        const { id } = this.props;
    
        // the Player object is created uniquely based on the id in props
        this.player = new window.YT.Player(`youtube-player-${id}`, {
          videoId: id,
          events: {
            onReady: this.onPlayerReady,
          },
        });
      };
    
      onPlayerReady = event => {
        event.target.playVideo();
      };
    
      render = () => {
        const { id } = this.props;
        return (
          <div className={classes.container}>
            <div id={`youtube-player-${id}`} className={classes.video} />
          </div>
        );
      };
    }
    
    export default YouTubeVideo;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

    0 讨论(0)
提交回复
热议问题