Here is the working code for Auto Play Youtube Video on Page load or in open bootstrap pop up:
<script type="text/javascript" src="https://www.youtube.com/player_api"></script>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
// 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.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '180',
width: '640',
videoId: 'JcO-vyfs_nc', //youtube video id
playerVars: { autoplay: 1 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.setVolume(100);
event.target.playVideo();
}
function stopVideo() {
player.stopVideo();
}
//video
</script>
<script type="text/javascript" src="https://www.youtube.com/player_api"></script>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
// 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.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '180',
width: '640',
videoId: 'JcO-vyfs_nc', //youtube video id
playerVars: { autoplay: 1 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.setVolume(100);
event.target.playVideo();
}
function stopVideo() {
player.stopVideo();
}
//video
</script>
No comments:
Post a Comment