2016-11-18 160 views
0

我想知道如何在沒有任何按鈕或控件的情況下實現視頻播放列表,只需播放一組視頻並在最後一次結束時再次開始。Javascript - 自動視頻播放列表

我發現這段關於可點擊播放列表的代碼片段:http://jsfiddle.net/e8CbF/ 但我真的不知道如何使它自動化。此外,該數組來自一個PHP變量,我該如何在代碼中使用它?

function loadVids(vidsArray){ 
for(var a=0,b,f=document.createDocumentFragment();b=vidsArray[a];++a){ 
    var c=document.createElement('div'); 
    c.textContent=b; 
    f.appendChild(c); 
} 
d.appendChild(f); 
} 
var video=document.createElement('video'),vids=['http://screen.digitalifts.com/screenfiles/video1.mp4','http://screen.digitalifts.com/screenfiles/video2.mp4'], /* Is it there that I should put the php array ? */ 
d=document.createElement('div'); 
d.onclick=function(e){if(e.target.parentNode==this){ 
video.src=e.target.textContent; 
video.play(); 
}} 
document.body.appendChild(video); 
document.body.appendChild(d); 
loadVids(vids); 

回答

1

你的代碼是一團糟。所以我完全重寫它。

<video src="" id="player"/> 

<script> 
var video=counter=0; 

videos=['<?php echo join("';'",$array);?>']; 

window.onload=()=>{ 
    //get the video frame 
    video=document.getElementById("player"); 
    //if the video ended, play next. 
    video.addEventListener("ended",play,false); 
    //start 
    play(); 
} 

var play=()=>{ 
    //add the video src 
    video.src=videos[counter]; 
    //play next video next time 
    counter++; 
    if(counter==videos.length){ 
     counter=0; 
    } 
}; 
</script> 
+0

謝謝!這樣確實更友好。我可以在「視頻= []」中添加回顯,就像我在html標籤中做的一樣?所以我可以給它我的數組的價值? – Ezhno

+0

Jep。但它必須有正確的格式。 –

+0

現在我的視頻源位於數組中。所以我想我應該做一個循環來將它們添加到一個字符串中,如下所示: 'loop() {list =「\」「。​​$ myarray [i]。」\「」。 「」; }'? – Ezhno