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
Related
This is the code I use for autoplaying youtube videos:
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '970',
width: '100%',
videoId: videos[index],
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
location.reload();
}
}
And the html:
<script src="http://www.youtube.com/player_api"></script>
videos[index] - A variable in which all the videos are stored.
Is there any variation of the code that can play videos from google drive (or basically any code that can autoplay the video and alert when it's finished, it has to be in js)
I have looked for solutions to my problem for a few hours now and I tried them all out but without any success.
Does anyone have an idea on how I could accomplish this through JavaScript?
The answer is NO because Google Drive doesn't have an API, and it doesn't work like Youtube.
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
}
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?
I have 2 working scripts, the first using the youtube player api:
(#container_home contains a logo showing after the video ends)
$(document).ready(function() {
$("#container_home").hide();
$("#player").hide();
});
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
controls: 'false',
videoId: 'kCdFqMJPqyQ',
playerVars: {
'showinfo': 0,
'rel': 0,
'controls': 0},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
$("#player").fadeTo( "fast" , 1);
$(".jay_load").hide();
}
function onClick(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
$("#player").hide();
$("#container_home").show(2000);
}
}
And the second tells me if I'm using a desktop computer or another device (using wurfl script):
$.getScript("//wurfl.io/wurfl.js")
.done(function() {
$.wurfl = WURFL;
if ($.wurfl.form_factor === 'Desktop') {
alert ('yes');
};
});
So my goal is to run the youtube script only on desktop browsers.
I tried to copy the first script and paste it over
alert ("yes");
But thats not working. I hope someone can help me.
Thanx!
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]);
}
}
}