How to detect when a youtube video finishes playing?
This can be done through the youtube player API:
http://jsfiddle.net/7Gznb/
Working example:
<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', {
width: '640',
height: '390',
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>
Detecting when a YouTube video has finished playing
You could use MediaPlayer
. It has an isPlaying
method to check if music/videos are currently playing. Just set a Boolean
to true while it's playing and check the isPlaying
method against that Boolean
to see if it changed from True
to False
.
Otherwise Youtube (or google) may have some specific API's
EDIT:
I looked into here; and it appears that there is an API for youtube.
you can use the method PlayerStateChangeListener
for the YoutubePlayer
class to get the listener and implement the method OnVideoEnded
How can I detect that YouTube has finished video playback from injected script (using chrome extension)?
Use ended DOM event on the video element:
var videoElement = document.querySelector('video');
videoElement.addEventListener('ended', function(event) {
console.log(event);
});
Note, Youtube site uses AJAX navigation, which means your extension's content script runs only one time when the site is opened in any given tab. Add a listener for yt-navigate-finish
to detect such navigation:
document.addEventListener('yt-navigate-finish', function(event) {
var videoElement = document.querySelector('video');
videoElement.addEventListener('ended', function(event) {
console.log(event);
});
});
Use devtools to see more events used by the page:
- in Chrome: open Elements inspector and switch to the "Event listeners" subpanel
or typegetEventListeners(document)
in the console (also works withwindow
and any DOM element) - in Firefox: open Inspector and click the
ev
orevent
bubble to the right of<html>
and other tags
Can Javascript detect when an embedded Youtube video has ended?
Here's what you're looking for:
http://jsfiddle.net/7Gznb/
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert('done');
//do something here
}
That's the Youtube API by the way
Event when YouTube video finished
Youtube has a JavaScript API: https://developers.google.com/youtube/js_api_reference
What you need is the onStateChange event, which will give you 0 when ended.
player.addEventListener("onStateChange", function(state){
if(state === 0){
// the video is end, do something here.
}
});
Detecting when a youtube video finishes in Android
You can get a callback when video has completed if you are using the youtube API.
So by ovveriding the following you can implement your code,
@Override
public void onVideoEnded() {
//Put your code here
}
Detecting when a youtube video finishes
I have the solution just in case helps someone. I only have to create the new YT.Player when I want the dialog to appear.
j$(document).ready(function(){
var Bnr_vars = [
{
"banner" : "teamworkRowersDLVidOpener",
"closer" : "teamworkRowersDLVidCloser",
"dialog" : "teamworkRowersDLVidDialog",
"auto" : "false" // auto open TRUE or FALSE?
}
];
lightboxCreate(Bnr_vars); // create lightboxes based on array above
j$('#' + Bnr_vars[0].banner).click(function(){
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
{
var info=new YT.Player('player', {
height: '338',
width: '600',
videoId: '_Zmr0cUeNBY',
playerVars : {
playerapiid:'player1',
rel: '0',
wmode: 'transparent',
modestbranding: '1'
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
j$('#player').html(info);
}
});
Related Topics
Rake Db:Create - Could Not Find a JavaScript Runtime
Programmatically Select Text in a Contenteditable HTML Element
Ruby Methods Within JavaScript Within Haml
Space Filling with Circles of Unequal Size
How to Enable Touch on Multiple Buttons Simultaneously in React Native
Angular 2 Dependency Injection in Es5 and Es6
How to Create an Object from an Array of Key-Value Pairs
Determine Which Element the Mouse Pointer Is on Top of in JavaScript
How to Plug a JavaScript Engine with Ruby and Nokogiri
How to Get All Arguments of a Function as Single Object Inside That Function
What's the Difference Between Console.Dir and Console.Log
Is JSON Hijacking Still an Issue in Modern Browsers
Combined Comparison/"Spaceship" Operator (<=>) in JavaScript
How Persistent Is Localstorage
Creating Range in JavaScript - Strange Syntax