JSFiddle - React, Tailwind, and code Playground
HTML
<div id="player"></div>
<br><br>
<input type="button" value="mute() " onclick="player.mute()" /><br>
<input type="button" value="unMute()" onclick="player.unMute()" /><br>
JavaScript
g = window;
// 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.
g.onYouTubeIframeAPIReady = function () {
alert("dfg");
g.player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '6B7mxnksT0Q',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
g.onPlayerReady = function(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
g.done = false;
g.onPlayerStateChange = function(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
done = true;
}
}
g.stopVideo = function () {
player.stopVideo();
}