首页 > 解决方案 > YouTube Java Script player.setPlaybackRate() API 不再工作

问题描述

Google 示例最能说明问题:

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

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

标签: youtube-iframe-api

解决方案


不知道为什么在YouTube 播放器演示网站上setPlaybackRate不起作用,但如果您尝试一下,它肯定会起作用。

这是我使用的代码,您可以检查工作的 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>


推荐阅读