How to Capture Youtube Video Start Using JavaScript API
Pro开发者_运维技巧gramming Language: Javascript and Youtube Javascript API
Problem: A website has multiple youtube videos placed on it using Javascript API. Call/Report to another function as soon as a user starts watching videos. Code I have so far:var youTubePlayerLoggers = {};
function onYouTubePlayerReady(playerId) {
var video = document.getElementById(playerId);
youTubePlayerLoggers[playerId] = function (newState) {
if (newState == 1) {
if (video && video.getDuration) {
if (video.getCurrentTime() == 0) {
UserStartedVideo('param1', 'param2');
}
}
}
};
video.addEventListener('onStateChange', 'youTubePlayerLoggers.' + playerId);
video.addEventListener('onError', 'onPlayerError');
}
function UserStartedVideo(param1,param2){
}
In the 5th line of the code I am checking if newState == 1 (i.e user has clicked on play button) then if the video.getCurrentTime ==0 --> It is the start of the video by user.
Issue I am having: I have multiple videos on the website. When I click on the play button of the first video, UserStartedVideo method is triggered. Similarly, UserStartedVideo method should be triggered as soon as I click on play button of the second video, but it is not happening.
Can anyone tell me where am I doing wrong or any otherway to capture start of the videos?
Have you checked http://code.google.com/apis/youtube/js_api_reference.html#Playback_status ?
how about setTimeout checking of player.getPlayerState() ?
精彩评论