Autostart a youtube video from Javascript - javascript

I am trying to automatically start a youtube video when it loads and when it finishes I want it to alert that it's done.
This is my HTML/JS
<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '0Bmhjf0rKe8',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert('done');
}
}
</script>
The part that alerts when the video is done is working but the auto-player doesn't. I HAVE TRIED TO PUT THE ?AUTOPLAY=1 TAG AFTER THE VIDEO URL BUT THAT DOESN'T SEEM TO WORK EITHER!
I have seen all the variations to this question and tried all the anwsers but none of them work.
Does anyone have any idea on what I should try next?

Thanks to cat my problem is solved!
All I had to do is go to chrome://flags/#autoplay-policy and select "No user gesture is required" in the dropdown menu under the "Autoplay policy" and then relaunch chrome.

player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '0Bmhjf0rKe8',
autoplay: 1,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}

Related

How to Dynamically update Youtube Player videoID

I have a js function that returns a random VideoID from a channel. I want to dynamically insert this ID into the youtubeAPI's videoID parameter.
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: <<INSERT RETURN OF FUNCTION HERE>>
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
Prueba utilizando esta linea :)
player.loadVideoById("id_video");

Iframe with embed Youtube Video stops after alert shows up?

I have the problem that my embed Youtube Video stops playing after a Popup like alerts or ion-select get called.
This is the html:
<iframe src='http://www.youtube.com/embed/aasdfsdf?showinfo=0&iv_load_policy=3&modestbranding=1&playsinline=1' frameborder="0" width="300" height="150" allowfullscreen no-padding webkit-playsinline playsinline></iframe>
I really cant show more code because there isnt more. Only a Button below the iframe witch pop up a alert. And if i do that the video stops and get load a second time.
How could i slove this issue?
TRY THIS DEMO
But its applicable on embeded youtube video
<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '0Bmhjf0rKe8',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert('completed');
}
}
</script>

YouTube API starts two videos in the same time

I have two videos that are controlled with YouTube api, everything works fine excepting that when the page loads the first video starts (this is normal) but on the same time you can hear the sound of the second video (this must start after first video ends or is manually closed) which can be better heard if you pause the first video...does anyone has a solution for this?
I think that this happens because both video are calling the onReady event.
It is a must to start the second video automatically when the first one ended or was closed.
<style>
.second-video {
display: none;
}
</style>
<div class="video-site">
<div class="first-video">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div id="player"></div>
</div>
<div class="second-video">
<div id="player2"></div>
</div>
</div>
<script src="http://www.youtube.com/player_api"></script>
<script>
// create youtube player
var player;
var player2;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'qCDxEif_9js',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player2 = new YT.Player('player2', {
height: '390',
width: '640',
videoId: '43jHG-yH9Gc',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
$('.close').click(function(){
$('.first-video').hide(3000);
event.target.stopVideo();
$('.second-video').fadeIn();
});
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
$('.first-video').hide(3000);
$('.second-video').fadeIn();
}
}
</script>
http://jsfiddle.net/yjrx8uyz/
I updated your fiddle http://jsfiddle.net/yjrx8uyz/3/
Basically you have to check which player has fired the ready event and closing event.
if(event.target == player)
Ref : How to detect when a youtube video finishes playing?

how to display lightbox after Video Play Finishes?

I have a youtube video.
I want to show a lightbox when it stops playing. I need this to be done using javascript/jQuery or PHP. Ajax is also fine.
I looked for a solution but didn't find one that worked.
If you can use youtube api then, something like this should work:
<script type="text/javascript">
$(document).ready(function() {
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'YmHAAqOsBqA',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if(event.data === 0) {
//completed playing
//open lightbox
$('#yourElementId a').lightBox();
}
}
});
</script>
Did you mean something like this.
Hope it helps

Example YouTube Playlist Code?

I'd like to create a playlist that loads all the videos a specific user has uploaded.
I was suggested to use the following code (assuming the account to pull in is YouTube):
loadPlaylist( { listType: 'user_uploads', list: 'youtube' } );
I have looked over the API pages as well: http://code.google.com/apis/youtube/js_api_reference.html
But I can't find an actual example code that uses load playlist. Being completely new to YouTube API I have no idea what type of wrapper code I need to make the above work. Something like this (of course I'm missing parts):
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
loadPlaylist( { listType: 'user_uploads', list: 'youtube' } );
</script>
Or if someone could provide working example with the original loadplaylist line that would be great, and I can work on the other details I need on my own from there.
-YouTube API Newbie
First of all , I suggest you to use IFRAME embed style, because it work on both desktop and mobile (iOS,Android,BB,Windows, very nice). The sample code is at Youtube IFRAME embed . This one contain the html code for you to get started.
Let's get into your question.
1 Let assume you get youtube player as
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
events: {
'onReady': onPlayerReady
}
});
don't care with videoId. just insert any valid youtube video's ID.
notice that we register 'onReady': onPlayerReady
2 Load your playlist into the player using onPlayerReady
function onPlayerReady(event) {
event.target.loadPlaylist({list: "UUPW9TMt0le6orPKdDwLR93w", index: 1, startSeconds: 10,suggestedQuality: "small"});
}
You can read more on Youtube JSAPI reference
Hope this work on you. ^^.
UPDATE
you can also specify the playlist in playerVars object.
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
playerVars: {
listType:'playlist',
list: 'UUPW9TMt0le6orPKdDwLR93w'
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
it has the feature
-make playlist of video id.
-to play next and previous.
-play,pause and stop
https://jsfiddle.net/Physcocybernatics/6dkw8e7r/
<div id="player"></div>
<div class="buttons">
<button class="button" id="play-button">PLAY</button>
<button class="button" id="pause-button">PAUSE</button>
<button class="button" id="previous">previous</button>
<button class="button" id="next">Next</button>
<button class="button" id="stop-button">STOP</button>
</div>
/**
* Put your video IDs in this array
*/
var videoIDs = [
'kJQP7kiw5Fk',
'2cv2ueYnKjg',
'nfs8NYg7yQM',
'ClU3fctbGls'
];
var player, currentVideoId = 0;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
playerVars: {
controls: 1,
showinfo: 0,
rel: 0,
showsearch: 0,
iv_load_policy: 3
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.loadVideoById(videoIDs[currentVideoId]);
// bind events
var playButton = document.getElementById("play-button");
playButton.addEventListener("click", function() {
player.playVideo();
});
var pauseButton = document.getElementById("pause-button");
pauseButton.addEventListener("click", function() {
player.pauseVideo();
});
var stopButton = document.getElementById("stop-button");
stopButton.addEventListener("click", function() {
player.stopVideo();
});
var next = document.getElementById("next");
next.addEventListener("click", function() {
player.nextVideo();
});
var pre = document.getElementById("previous");
pre.addEventListener("click", function() {
player.previousVideo();
});
player.loadPlaylist( {
playlist:videoIDs
} );
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
currentVideoId++;
if (currentVideoId < videoIDs.length) {
player.loadVideoById(videoIDs[currentVideoId]);
}
}
}

Categories