YouTube Java脚本player.setPlaybackRate()API不再起作用

时间:2019-01-23 18:35:27

标签: youtube-iframe-api

Google示例最能说明问题:

https://developers.google.com/youtube/youtube_player_demo

更改“费率”,您将看到视频速率/速度没有变化。

1 个答案:

答案 0 :(得分:0)

不确定setPlaybackRateYouTube Player Demo website中为何不起作用,但是如果您尝试使用,肯定会起作用。

这是我使用的代码,您可以检查working jsfiddle

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;

function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    height: '360',
    width: '640',
    videoId: '00vnln25HBg',
    playerVars: {
      'autoplay': 1,
      'loop': 1,
      'mute': 1
    },
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
  event.target.playVideo();
}

// 5. The API calls this function when the player's state changes.
//    Here I set the "setPlaybackRate" value to "2".

function onPlayerStateChange(event) {
  player.setPlaybackRate(2);
}

function stopVideo() {
  player.stopVideo();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="player"></div>