對由Offbeatmammal on febr. 2 2014編寫的代碼作出反應。陣列中最後一個視頻結束後的停止循環
<head>
....
<script>
var videos = new Array("BigBuck.m4v","Video.mp4","BigBuck.m4v","Video2.mp4");
var currentVideo = 0;
function nextVideo() {
// get the element
videoPlayer = document.getElementById("play-video")
// remove the event listener, if there is one
videoPlayer.removeEventListener('ended',nextVideo,false);
// update the source with the currentVideo from the videos array
videoPlayer.src = videos[currentVideo];
// play the video
videoPlayer.play()
// increment the currentVideo, looping at the end of the array
currentVideo = (currentVideo + 1) % videos.length
// add an event listener so when the video ends it will call the nextVideo function again
videoPlayer.addEventListener('ended', nextVideo,false);
}
</script>
</head>
<body>
...
<div class="video-player">
<video id="play-video" width="588" height="318" controls autobuffer muted>
Your browser does not support the video tag.
</video> <!--end video container -->
</div>
<script>
// call the video player
nextVideo()
</script>
如何在陣列中最後一個視頻播放完畢後跳出循環?我嘗試了一切。
我能做這個工作的唯一方法就是這樣的:我在數組的末尾添加了一個空項目。比在if語句中,我設置的電流等於最後一項。它有一個讓最後一個視頻播放直到完成的技巧,只有在這之後停止循環並執行其他操作。但是,這是很笨拙:
這是我的工作代碼:
<script type="text/javascript">
var videos = ["video/1964","video/1964deVos",""];
var current = 0;
var videoPlayer = document.getElementById("videoplayer");
var last = (current + 1) % videos.length-1;
function vid() {
if (videoPlayer.canPlayType('video/mp4')) {
mimetype = '.mp4';
}
if (videoPlayer.canPlayType('video/webm')) {
mimetype = '.webm';
}
videoPlayer.removeEventListener('ended',vid,false);
videoPlayer.src = videos[current]+mimetype;
videoPlayer.play();
current = (current + 1) % videos.length;
if (current==last){
$("#media").hide();
$('#select').delay(1000).fadeIn(1000);
}
videoPlayer.addEventListener('ended', vid,false);
}
$(document).ready(function(){
vid();
});
什麼是「一切」,你必須給使用的詳細信息,至少有點代碼 –
我更新了我原來的問題。 –