YouTube Java Script player.setPlaybackRate() API no longer working

↘锁芯ラ 提交于 2020-01-04 03:25:28

问题


The Google example demonstrates the problem best:

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

Change the "Rate" and you will see the video rate/speed does not change.


回答1:


Not sure why the setPlaybackRate doesn't work in the YouTube Player Demo website, but it surely works if your try it.

This is the code I used and you can check the 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>


来源:https://stackoverflow.com/questions/54333607/youtube-java-script-player-setplaybackrate-api-no-longer-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!