1

I have used the YouTube video API in JavaScript

And play pause and stop events I have used

Now I got total current time which total watched time, but my main problem is this:

Watch a video for a few seconds and fast forward, then watch another few seconds and see what time it records.... If I watch a video for 5 seconds, then fast forward to minute 10 and watch another 5 seconds, the time I actually watched the video is 10 seconds, not 10 minutes.

Here is my code :

function onytplayerStateChange(event) 
{ 
 //alert("Status=>"+event.data); 
 if(event. data == 1) { 
 if(Math.floor(event.target.A.currentTime) == 0) { 
 previous_track_time = current_track_time = 0; 
 total_track_time = event.target.A.duration; 
 } else { 
 previous_track_time = current_track_time; 
 current_track_time = event.target.A.currentTime; 
 } 
 } 
 if(event.data == 2) 
 { 
 current_track_time = event.target.A.currentTime; 
 total_track_time =(event.target.A.duration-(current_track_time-previous_track_time)); 
 previous_track_time = current_track_time; 
 }
} 

Can you fix that please?

mkj
2,8615 gold badges25 silver badges28 bronze badges
asked Oct 2, 2014 at 6:14
3
  • because you watch 10 seconds of a video. without code it's hard to help you Commented Oct 2, 2014 at 8:16
  • function onytplayerStateChange(event) { //alert("Status=>"+event.data); if(event. data == 1) { if(Math.floor(event.target.A.currentTime) == 0) { previous_track_time = current_track_time = 0; total_track_time = event.target.A.duration; } else { previous_track_time = current_track_time; current_track_time = event.target.A.currentTime; } } if(event.data == 2) { current_track_time = event.target.A.currentTime; total_track_time =(event.target.A.duration-(current_track_time-previous_track_time)); previous_track_time = current_track_time; } Commented Oct 2, 2014 at 8:20
  • @martialdidit i have added code please check it please help me Commented Oct 2, 2014 at 8:21

1 Answer 1

2

If you want the time only watched on a video by a user you need to use a counter. I also simplify your code.

The function onPlayerStateChange:

function onPlayerStateChange(event) {
 if (event.data == YT.PlayerState.PLAYING) {
 //player play
 timer = setInterval(
 function() {
 seconds++;
 console.log("you watch: "+ seconds +" seconds of the video");
 }, 1000
 );
 } else {
 //player pause
 clearInterval(timer);
 }
}

The output : I have see only 8 seconds of the video, the 4 first seconds, and 4 seconds at 2:03

enter image description here

There is a live example (UPDATED)

The full code:

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 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: 'l-gQLqv9f4o',
 events: {
 'onReady': onPlayerReady,
 'onStateChange': onPlayerStateChange
 }
 });
}
// 4. The API will call this function when the video player is ready.
var seconds = 0;
var timer;
function onPlayerReady(event) {
 event.target.playVideo();
}
function onPlayerStateChange(event) {
 if (event.data == YT.PlayerState.PLAYING) {
 //player play
 timer = setInterval(
 function() {
 seconds++;
 console.log("you watch: "+ seconds +" seconds of the video");
 }, 1000
 );
 } else {
 //player pause
 clearInterval(timer);
 }
}
function stopVideo() {
 player.stopVideo();
}
answered Oct 2, 2014 at 9:29
Sign up to request clarification or add additional context in comments.

3 Comments

Hello, i want only total video watch time. Like if video is total time is 50 seconds and user start watch from 0 second to 10 seconds then after user fast forward video from 10 second to 20 second then continue to end video so total watched time is 40 because user have skip from 10 seconds to 20 seconds. Now i Hope now you are understand what i need.
@user3232409 i updated my answer (code, live example...) A good question ! :)
@user3232409 at least, you can accept the answer for future viewers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.