【问题标题】:creating a youtube iframe video from my constructor object so I can create multiple videos从我的构造函数对象创建一个 youtube iframe 视频,这样我就可以创建多个视频
【发布时间】:2012-12-30 13:46:13
【问题描述】:

我正在尝试使用 youtube iframe api 以及我正在处理的构造函数创建一个 youtube 视频,但我遇到了一个绊脚石。目前在我的 Player 函数中,我创建了一些默认属性,然后我将一些新属性传递到我的新对象中,以便通过扩展默认属性和新属性来创建播放器。我现在的问题是我不确定我实际上是如何初始化 youtube 视频的?我不确定player = new YT.Player('player', {应该去哪里?

这是我正在开发的 JS 和 jsFiddle http://jsfiddle.net/kyllle/6zuh5/7/

function Player(options) {  
  var $player = $(options.id);

  var defaults = {
    height: '100',
    width: '200',
    videoId: 'u1zgFlCw8Aw',
    events: {
      'onReady': onPlayerReady

    },
    playerVars: {
      modestbranding: 0,
      controls: 0, //remove controls
      showinfo: 0,
      enablejsapi : 1,
      iv_load_policy: 3
    }
  };
     
  var combinedOptions = _.extend(defaults, options);
  console.log('Combined Options', combinedOptions);

  return {    
    pause: function () {      
      $player.pauseVideo();    
    },
        
    seek: function () {       
      //$player.seekTo();          
    },
        
    destroy: function () {      
      $player.destroy();    
    },
        
    changeVideo: function () {      
      $player.stopVideo();    
    }  
  }
};

function onPlayerReady() {
  console.log('player fired');
}

var myPlayer = new Player({  
    id: '#divId',
    autoPlay: true,
    videoId: 'asdadads'
});

【问题讨论】:

    标签: javascript jquery youtube-api


    【解决方案1】:

    将其放在您希望视频出现的位置:

    <div width="200" height="100" onclick="loadVideo();"></div>
    

    把它放在页尾:

    <script>
    function loadVideo() { // 2. This code loads the IFrame Player API code asynchronously.
     var tag = document.createElement('script');
     tag.src = "http://www.youtube.com/player_api";
     var firstScriptTag = document.getElementsByTagName('script')[0];
     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); }
     // func loadVideo
     // 3. This function creates an <iframe> (and YouTube player)
     // after the API code downloads.
     var player;
     function onYouTubePlayerAPIReady() { player = new YT.Player('player', { height: '100', playerVars: { 'autoplay': 1, rel: 0 }, width: '200', videoId: 'mXtJ9BbeGB4', events: { 'onReady': onPlayerReady } }); }
     // 4. The API will call this function when the video player is ready.
     function onPlayerReady(event) { event.target.playVideo(); }
    </script>
    

    【讨论】:

    • 嘿,但我正在尝试使用构造器对象来创建 youtube iframe,以便我可以学习如何使用它并在需要时创建多个视频?
    • 我想将 new YT.Player('player', { 添加到 Player 函数中,假设它将被放置在哪里?
    • 您的问题是:我现在的问题是我不确定我如何实际初始化 youtube 视频?
    • 好的,如何使用我创建的构造函数初始化视频?我是否从 Player 函数内部对其进行初始化?
    • 播放器功能后继承
    【解决方案2】:

    changeVideo 函数之后添加一个名为init 的函数。然后在实例化 Player 之后,您将在该新实例上调用 init。 (i.e. myPlayer.init())

    这是我的想法的一个例子:我省略了下划线,而是使用了 jQuery 的 $.extend 函数,这样我就不用多管闲事了:

    <html>
    <head>
        <title>Video Player</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            function Player(options) {  
    
            var defaults = {
                height: '100',
                width: '200',
                videoId: 'u1zgFlCw8Aw',
                events: { 'onReady': null},
                playerVars: {
                      modestbranding: 0,
                      controls: 0, //remove controls
                      showinfo: 0,
                      enablejsapi : 1,
                      iv_load_policy: 3
                 }
              };
    
              var combinedOptions = $.extend(defaults, options);
              console.log('Combined Options', combinedOptions);
    
              return {
                player: null,
    
                pause: function () {      
                    this.player.pauseVideo();    
                },
    
                seek: function () {       
                  //this.player.seekTo();          
                },
    
                destroy: function () {      
                    this.player.destroy();    
                },
    
                changeVideo: function () {      
                    this.player.stopVideo();    
                },
    
                onPlayerReady: function() {
                    this.player.play();
                },
    
                init: function() {
                    this.player = new YT.Player(combinedOptions.id, {
                    height: combinedOptions.height,
                    width: combinedOptions.width,
                    videoId: combinedOptions.videoId,
                    events: {
                        'onReady': this.onPlayerReady,
                        'onStateChange': null
                    }
                    });
                }
                 }
            };
    
    
            var myPlayer;
            function onYouTubeIframeAPIReady() {
                myPlayer = new Player({  
                    id: 'divId',
                    autoPlay: true,
                    videoId: 'NeGe7lVrXb0'
                });
    
                myPlayer.init();
    
            }
    
            $(document).ready(function() {
                var tag = document.createElement('script');
                tag.src = "https://www.youtube.com/iframe_api";
                var firstScriptTag = document.getElementsByTagName('script')[0];
                firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);    
            });
    
    
        </script>
    </head>
    <body>
        <div id="divId"></div>
    </body>
    </html>
    

    【讨论】:

    • 嘿,你能证明一下吗?我试图弄清楚这是如何工作的
    • 好的。我添加了演示 init 函数的代码。我使用了 jQuery 的 $.extend,这样我就不必再使用下划线了。
    • 自从我注意到赏金有一条评论说您正在寻找对可靠来源的引用,我想我会添加this link。在模块模式部分下,有一个 ExtJS 示例,它使用类似于我的方式的 init 方法。我觉得如何初始化这样的东西的问题很笼统,所以我认为有很多同样好的方法可以做到这一点。
    【解决方案3】:

    一个 Player 类的工作示例,用于创建和控制单独的玩家。

    JSFiddle Example

    HTML:

    <div id="divId1"></div>
    <a href="#" id="play1">Play</a>
    
    <a href="#" id="pause1">Pause</a>
    
    <a href="#" id="stop1">Stop</a>
    <br>
    <div id="divId2"></div>
    <a href="#" id="play2">Play</a>
    
    <a href="#" id="pause2">Pause</a>
    
    <a href="#" id="stop2">Stop</a>
    
    <script>
      var tag = document.createElement('script');
      tag.src = "//www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    </script>
    

    Javascript

    var Player = (function(){
      //private static
      var defaults = {
        height: '100',
        width: '200',
        videoId: 'u1zgFlCw8Aw',
        events: {},
        playerVars: {
          modestbranding: 0,
          controls: 0, //remove controls
          showinfo: 0,
          enablejsapi : 1,
          iv_load_policy: 3
        }
      };
    
        var constructor = function(options){
            this.options = _.extend(defaults, options);
    
            this.pause = function(event){
                event.target.pauseVideo()
            }
    
            if(this.options.autoPlay){
                this.options.events['onReady'] = function(event){
                    event.target.playVideo()
                }
            }
            this.player = new YT.Player(this.options.id,this.options)
            //pause on click
            $(this.options.pauseID).bind('click',function(event){
                this.player.pauseVideo()
            }.bind(this))
            //play on click
            $(this.options.playID).bind('click',function(event){
                this.player.playVideo()
            }.bind(this))
            //stop on click
            $(this.options.stopID).bind('click',function(event){
                this.player.stopVideo()
            }.bind(this))
        }
    
        return constructor;
    })() //function(){
    
    $(document).ready(function(){
      var myPlayer = new Player({  
        id: 'divId1',
        pauseID:'#pause1',
        playID:'#play1',
        stopID:'#stop1',
        autoPlay: false,
        videoId: 'oe_mGl1f4xs'
      });
    
      var myPlayer2 = new Player({  
        id: 'divId2',
        pauseID:'#pause2',
        playID:'#play2',
        stopID:'#stop2',
        autoPlay: false,
        videoId: 'u1zgFlCw8Aw'
      });
    })
    

    创建 Player 类的代码基于this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多