douyi5822 2019-07-13 21:08
浏览 566

HTML自动播放视频并从文件夹继续循环

I want to run all the videos that are in the folder, autoplay them and repeat them continuously.

I have found some javascript code and it works perfectly but it has to be included in the javascript code list, is there a way to change this coding to read all the videos in a folder.

this is the javascript code :

<div class="main">  
    <video src="video\\video1.mp4" id="myvideo" width="100%" height="100%" controls autoplay poster="gambar\logo.png">
</video>

<script>
    var myvid = document.getElementById('myvideo');
var myvids = [
  "video\\video1.mp4", 
  "video\\video2.mp4",
  "video\\video3.mp4"
  ];
var activeVideo = 0;

myvid.addEventListener('ended', function(e) {
  // update the new active video index
  activeVideo = (++activeVideo) % myvids.length;

  // update the video source and play
  myvid.src = myvids[activeVideo];
  myvid.play();
});
</script>
</div>  

if indeed there is another way I will also accept it, maybe using a database to read the file or other coding. Please help.

  • 写回答

1条回答 默认 最新

  • doubo6658 2019-07-14 02:43
    关注

    Assuming you are using PHP ,

    <?php
    $directory='videos'; // Add your Directory here
    $path    = './'.$directory.'/';
    $allFiles = scandir($path,1);
    $files = array_diff($allFiles, array('.', '..'));
    print_r($files)
    ?>
    
    <div class="main">  
        <video src="<?php echo 'videos/'.$files[0];?>" id="myvideo" width="100%" height="100%" controls autoplay  poster="gambar\logo.png">
    </video>
    <script type='text/javascript'>
      var directory = '<?php echo $directory;?>';
        var myvids = <?php echo json_encode($files); ?>;
        index=0;
        document.getElementById('myvideo').addEventListener('ended',myHandler,false);
        function myHandler(e) {
         index++;
           // For Repeating all video files 
           if(index>=myvids.length)
            index=0;
    
         var vid = document.getElementById("myvideo");
           vid.src = directory+'/'+myvids[index];
        }
    
    </script>
    </div>  
    

    Hope this will help you ! .

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料