使用 youtube api 加载多个视频播放器 [英] loading multiple video players with youtube api

查看:27
本文介绍了使用 youtube api 加载多个视频播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 youtube 的 API 加载多个视频.这是我第一次使用它,所以我不确定我做错了什么,但这就是我正在尝试的:

I need to load more than one video with youtube's API. This is my first time using it so I'm not sure what I'm doing wrong, but this is what I'm trying:

  var player;
  var player2;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      videoId: 'hdy78ehsjdi'
    });
    player2 = new YT.Player('player', {
      videoId: '81hdjskilct'
    });
  }

推荐答案

由于 onYouTubeIframeAPIReady 函数应该只调用一次,因此可以使用以下方法:

Since onYouTubeIframeAPIReady function is supposed to called only once the following approach could be used:

  • 初始化并保存视频播放器信息(ControlId,width,height,VideoId) 数组

调用 onYouTubeIframeAPIReady 函数来创建所有视频球员

call onYouTubeIframeAPIReady function to create all the video players

var playerInfoList = [{id:'player',height:'390',width:'640',videoId:'M7lc1UVf-VE'},{id:'player1',height:'390',width:'640',videoId:'M7lc1UVf-VE'}];

      function onYouTubeIframeAPIReady() {
        if(typeof playerInfoList === 'undefined')
           return; 

        for(var i = 0; i < playerInfoList.length;i++) {
          var curplayer = createPlayer(playerInfoList[i]);
        }   
      }
      function createPlayer(playerInfo) {
          return new YT.Player(playerInfo.id, {
             height: playerInfo.height,
             width: playerInfo.width,
             videoId: playerInfo.videoId
          });
      }

这篇关于使用 youtube api 加载多个视频播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆