本文介绍了如何在HTML5中播放视频数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我发现这段代码可以背靠背播放一系列视频,但我面临一个问题 我错了请帮助 < !DOCTYPE html > < html xmlns = http://www.w3.org/1999/xhtml > < head > < title > < / title > < script type = text / javascript > var videoSource = new Array(); videoSource [0] ='videos / 1.mp4'; videoSource [1] ='videos / 2.mp4'; var videoCount = videoSource.length; document.getElementById(myVideo)。setAttribute(src,videoSource [0]); 函数videoPlay(videoNum) { document.getElementById(myVideo)。setAttribute(src,videoSource [videoNum]); document.getElementById(myVideo)。load(); document.getElementById(myVideo)。play(); } document.getElementById('myVideo')。addEventListener('ended',myHandler,false); 函数myHandler(){ i ++; if(i ==(videoCount-1)){ i = 0; videoPlay(i); } else { videoPlay(i); } } < / script > < / head > < 正文 > < video controls = controls id = myVideo > < / video > < / body > < / html > 解决方案 有一些问题: a)你的JavaScript在任何元素加载之前运行。将您的脚本放在 body 标记的末尾,或者将其包装在 window.onload 事件中,如下所示: window .onload = function ( e){ // 您的脚本 } b)变量 i 未定义。 c) autoplay 什么都不做,可以删除;你必须使用JavaScript播放第一个视频。 var videoSource = new Array (); videoSource [ 0 ] = ' http: //devdoodle.net/a/beep.mp3' ; videoSource [ 1 ] = ' http: //devdoodle.net/a/beep.mp3' ; var i = 0 ; // define i var videoCount = videoSource.length; function videoPlay(videoNum){ document .getElementById( myVideo)。setAttribute( src,videoSource [videoNum]); document .getElementById( myVideo)负载(); document .getElementById( myVideo)播放(); } document .getElementById(' myVideo')。addEventListener(' ends',myHandler,假); videoPlay( 0 ); // 播放视频 function myHandler(){ i ++; if (i ==(videoCount - 1 )){i = 0 ; videoPlay(i); } 其他 { videoPlay(i); } } I found this code to Play a array of videos back to back but i am facing a issuewhere i am wrong pls help<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <script type="text/javascript"> var videoSource = new Array(); videoSource[0] = 'videos/1.mp4'; videoSource[1] = 'videos/2.mp4';var videoCount = videoSource.length; document.getElementById("myVideo").setAttribute("src",videoSource[0]); function videoPlay(videoNum) {document.getElementById("myVideo").setAttribute("src",videoSource[videoNum]);document.getElementById("myVideo").load();document.getElementById("myVideo").play(); } document.getElementById('myVideo').addEventListener('ended',myHandler,false);function myHandler() {i++;if(i == (videoCount-1)){i = 0;videoPlay(i);}else{videoPlay(i);} } </script></head><body> <video controls="controls" id="myVideo" > </video></body></html> 解决方案 There are a few problems:a) Your JavaScript runs before any of the elements has been loaded. Either put your script at the end of your body tag, or wrap it in the window.onload event like this:window.onload = function(e) { // your script here}b) The variable i is undefined.c) The autoplay doesn't do anything and can be removed; you have to play the first video using JavaScript.var videoSource = new Array();videoSource[0] = 'http://devdoodle.net/a/beep.mp3';videoSource[1] = 'http://devdoodle.net/a/beep.mp3';var i = 0; // define ivar videoCount = videoSource.length;function videoPlay(videoNum) { document.getElementById("myVideo").setAttribute("src", videoSource[videoNum]); document.getElementById("myVideo").load(); document.getElementById("myVideo").play();}document.getElementById('myVideo').addEventListener('ended', myHandler, false);videoPlay(0); // play the videofunction myHandler() { i++; if (i == (videoCount - 1)) { i = 0; videoPlay(i); } else { videoPlay(i); }} 这篇关于如何在HTML5中播放视频数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 21:53