kiểm tra cơ bản là đơn giản, chờ cho sự kiện ended
. Điều này rất đơn giản, bạn chỉ có thể google nó.
Bây giờ, hãy kiểm tra xem người dùng đó đã phát video đầy đủ có cần phân tích sâu rộng không nếu bạn chơi từng giây một. Đó không phải là cần thiết tuy nhiên, nó phải là đủ dùng rằng:
- chơi như nhiều giây như video dài
- chơi đến cuối của video
đoạn này thể hiện chính xác điều đó. Video sẽ không được đánh dấu là đã phát hoàn toàn nếu bạn chỉ cần bỏ qua đến cuối video. Chơi đầu hơn và hơn cũng sẽ không đánh dấu nó chơi đầy đủ:
var video = document.getElementById("video");
var timeStarted = -1;
var timePlayed = 0;
var duration = 0;
// If video metadata is laoded get duration
if(video.readyState > 0)
getDuration.call(video);
//If metadata not loaded, use event to get it
else
{
video.addEventListener('loadedmetadata', getDuration);
}
// remember time user started the video
function videoStartedPlaying() {
timeStarted = new Date().getTime()/1000;
}
function videoStoppedPlaying(event) {
// Start time less then zero means stop event was fired vidout start event
if(timeStarted>0) {
var playedFor = new Date().getTime()/1000 - timeStarted;
timeStarted = -1;
// add the new ammount of seconds played
timePlayed+=playedFor;
}
document.getElementById("played").innerHTML = Math.round(timePlayed)+"";
// Count as complete only if end of video was reached
if(timePlayed>=duration && event.type=="ended") {
document.getElementById("status").className="complete";
}
}
function getDuration() {
duration = video.duration;
document.getElementById("duration").appendChild(new Text(Math.round(duration)+""));
console.log("Duration: ", duration);
}
video.addEventListener("play", videoStartedPlaying);
video.addEventListener("playing", videoStartedPlaying);
video.addEventListener("ended", videoStoppedPlaying);
video.addEventListener("pause", videoStoppedPlaying);
#status span.status {
display: none;
font-weight: bold;
}
span.status.complete {
color: green;
}
span.status.incomplete {
color: red;
}
#status.complete span.status.complete {
display: inline;
}
#status.incomplete span.status.incomplete {
display: inline;
}
<video width="200" controls="true" poster="" id="video">
<source type="video/mp4" src="http://www.w3schools.com/html/mov_bbb.mp4"></source>
</video>
<div id="status" class="incomplete">
<span>Play status: </span>
<span class="status complete">COMPLETE</span>
<span class="status incomplete">INCOMPLETE</span>
<br />
</div>
<div>
<span id="played">0</span> seconds out of
<span id="duration"></span> seconds. (only updates when the video pauses)
</div>
Cũng om jsFiddle:
https://jsfiddle.net/p56a1r45/2/
Sau đó, bạn có thể kết nối này để Google Analytics để xem có bao nhiêu người sử dụng video chơi . mã đơn giản từ google analytics website:
ga('send', 'event', 'Videos', 'play', 'Video name');
Nguồn
2016-05-18 08:52:13
Bạn không [kiểm tra MDN đầu tiên] (https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events)? Các câu hỏi lập trình thuộc về SO. – doug65536
Đây là một giải pháp tôi tìm thấy, cảm ơn tất cả 'document.getElementById ('myVideo'). AddEventListener ('ended', myHandler, false); chức năng myHandler (e) { cảnh báo ("đã hoàn tất"); // Bạn muốn làm gì sau sự kiện } ' – mrana
Bạn đang cố gắng xác định xem mỗi giây của video đã được phát (không có cách nào để biết liệu video đó có được xem) hay chơi chấm dứt ở cuối hay gì? Yêu cầu của bạn không rõ ràng. – fixer1234