Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't play YouTube video through iframe API when video is unmute

Since Chrome update to version 72, my custom player that runs over the YouTube Iframe API stopped working. It still works perfectly on Firefox or Chrome <= 71.

Using the code below, when function play() is triggered, the video starts buffering and then stops, without playing.

function onYouTubeIframeAPIReady() {
  ytIframe = $("#player")[0];
  ytPlayer = new YT.Player(ytIframe, {
    events: {
      'onReady': () => {},
      'onStateChange': () => {}
    }
  });
}

function play() {
  ytPlayer.playVideo();
}

The only way to make it work is to embed the video using the mute=1 url param on the iframe src. But even when I do this, if I try to unmute the player after the playVideo (using ytPlayer.unMute()), the video stops again.

Any ideas on what has changed with this Chrome 72 update? Is this a YouTube/Chrome bug or is it an expected behavior?

Thanks!

like image 548
Pedro Barros Avatar asked Feb 18 '19 19:02

Pedro Barros


People also ask

How do I unmute YouTube iframe?

Until and unless you have allow="autoplay;" in your iframe tag you wont be able to unmute the video. Add this attribute and further unMute function will work. Show activity on this post. Usually, you can just click the mute button to unmute it.

Can you play videos with YouTube API?

The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript. Using the API's JavaScript functions, you can queue videos for playback; play, pause, or stop those videos; adjust the player volume; or retrieve information about the video being played.

How do I Autoplay YouTube iframe?

To make an embedded video autoplay, add "&autoplay=1" to the video's embed code right after the video ID (the series of letters that follows "embed/").

What can I do with the YouTube iframe API?

Using the Youtube IFrame API’s JavaScript functions you can control video playback (play, pause, or stop videos; adjust the player volume; or retrieve information about the video being played. You can also add event listeners that will execute in response to certain player events, such as a player state change or a video playback quality change.

What can I do with the YouTube API?

onPlayerReady – The API will call this function when the YouTube player is ready. You can write events for your custom buttons and perform operations on a YouTube player like playing a video, pausing a currently playing video, etc.

How to embed a YouTube video player on your website?

Share it! The Youtube IFrame API allows you to embed a YouTube video player (using an IFrame to contain the video) on your website and control the player using their JavaScript API that works across IFrames.

How does the onyoutubeiframeapiready function work?

The onYouTubeIframeAPIReady function will execute as soon as the player API code downloads. This portion of the code defines a global variable, player, which refers to the video player you are embedding, and the function then constructs the video player object.


1 Answers

"player.play()" doesn't work on latest version chrome, we can solve the issue by adding the allow="autoplay" attribute to your parent iframe

To know the root cause, you can check: "What's changing?" @ https://developers.google.com/web/updates/2019/01/user-activation

like image 189
user1729031 Avatar answered Sep 22 '22 20:09

user1729031