3

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.

2 Answers2

0

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>
Mauricio Arias Olave
  • 2,259
  • 4
  • 25
  • 70
  • It may work from within a player state change event handler (as in your example JS). It doesn't work however if used in a button click event handler. I would be happy if you could prove me wrong. – AlexG Mar 28 '19 at 12:32
  • @AlexG true. I found this website called [How to Control YouTube's Video Player with JavaScript](https://demo.tutorialzine.com/2015/08/how-to-control-youtubes-video-player-with-javascript/), but even there, the setPlaybackRate doesn't seems to work neither. Maybe this is another bug that YouTube iframe API has or any undocumented revision. – Mauricio Arias Olave Mar 29 '19 at 21:33
  • 1
    It used to work a few months ago. I made use of this method in my [AB Loop Player](https://agrahn.gitlab.io/ABLoopPlayer) where it worked flawlessly for more than two years. – AlexG Mar 29 '19 at 21:45
0

Looks like you just have to explicitly set the rate format:

let rate = 2;
player.setPlaybackRate(parseInt(rate));
dayo9
  • 1
  • 1