javascript - 如何设置youtube api的播放质量

标签 javascript youtube-api

我的网站上有 2 位 YouTube 播放器。一切正常,但我无法设置播放质量。我究竟做错了什么?

var playerInfoList = [{
  id: 'player',
  height: '390',
  width: '640',
  videoId: 'jU1_0T79Lew',
  playerVars: {
    'autoplay': 1,
    'autohide': 1,
    'showinfo': 0,
  },
  events: {
    'onReady': onPlayerReady
  }
}, {
  id: 'player1',
  height: '390',
  width: '640',
  videoId: 'u-OxxLMvOXg',
  playerVars: {
    'autoplay': 0,
    'autohide': 1,
    'showinfo': 0,
  }
}];

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,
    playerVars: playerInfo.playerVars,
    events: playerInfo.events
  });
}


// The API will call this function when the video player is ready.
function onPlayerReady(event) {
  playerInfo.id.setPlaybackQuality('hd1080'); // Here we set the quality (yay!)
}

最佳答案

Playback quality

player.getPlaybackQuality():String

  • This function retrieves the actual video quality of the current video. Possible return values are highres, hd1080, hd720, large, medium and small. It will also return undefined if there is no current video.

player.setPlaybackQuality(suggestedQuality:String):Void

  • This function sets the suggested video quality for the current video. The function causes the video to reload at its current position in the new quality. If the playback quality does change, it will only change for the video being played. Calling this function does not guarantee that the playback quality will actually change. However, if the playback quality does change, the onPlaybackQualityChange event will fire, and your code should respond to the event rather than the fact that it called the setPlaybackQuality function.

When you suggest a playback quality for a video, the suggested quality will only be in effect for that video. You should select a playback quality that corresponds to the size of your video player. For example, if your page displays a 1280px by 720px video player, a hd720 quality video will actually look better than an hd1080 quality video. We recommend calling the getAvailableQualityLevels() function to determine which quality levels are available for a video

注意

If you call the setPlaybackQuality function with a suggestedQuality level that is not available for the video, then the quality will be set to the next lowest level that is available. For example, if you request a quality level of large, and that is unavailable, then the playback quality will be set to medium (as long as that quality level is available).

基于本教程:How to Control YouTube's Video Player with JavaScript

var player;

function onYouTubeIframeAPIReady() {
player = new YT.Player('video-placeholder', {
width: 600,
height: 400,
videoId: 'Xa0Q0J5tOP0',
playerVars: {
color: 'white',
playlist: 'taJ60kskkns,FG0fTKAqZ5g'
},
events: {
onReady: initialize
}
});
}

更改视频质量

$('#quality').on('change', function () {
player.setPlaybackQuality($(this).val());
});

尝试将您的 playerInfo 设置为全局变量,并按照上面的教程来帮助您设置嵌入式 YouTube 视频。

希望这有帮助。

关于javascript - 如何设置youtube api的播放质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846208/

相关文章:

javascript - 如何使用 jQuery 显示/隐藏事件重新加载 jQuery 幻灯片?

ios - YTPlayerView 不在模拟器上播放内容(解码失败,错误代码)

jquery - 将youtube视频添加到网络应用

javascript - 将数据从 Cloudant/CouchDB 导出到 CSV

javascript - 谷歌地图方向 - 在 IE7 中不居中/缩放

windows-phone-8 - Google oauth api 在 google api dot net 客户端中注销问题

google-api - 使用 YouTube api v3 搜索我的视频时的 invalidSearchFilter

youtube - 更新免费的 youtube 画廊以使用 Youtube 3.0 API

javascript - 获取位于隐藏父级的元素的尺寸

javascript - 按下按钮时写入文件