4

我正在尝试使用 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'
});
4

3 回答 3

4

在函数之后添加一个调用initchangeVideo函数。然后在实例化 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>
于 2013-03-12T18:44:18.860 回答
0

Player 类的工作示例,用于创建和控制单独的播放器。

JSFiddle 示例

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 类的代码基于这个问题

于 2013-03-17T20:46:32.913 回答
0

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

<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>
于 2013-01-16T09:37:30.583 回答