2017-09-19 18 views
2

私のコードのすべてが機能します。それはちょうど現在のものを終えた後に次の歌/ビデオに切り替えません。私はonendedイベントハンドラを(タグとJavaScriptで)追加しようとしましたが失敗しました。私もjQueryを試しましたが、うまくいきませんでした。何らかの理由で、曲の最後に曲が変わらない。代わりに、同じ曲を繰り返し再生します。Javascript onendがビデオの終わりを検出しています

<video id="vid" src="main/" playsinline autoplay> 

(あなたのコードは以下となります。あなたは終了イベントdoc)をトリガしたい場合は

<video id="vid" src="main/" playsinline autoplay loop> 
 
\t \t <script> 
 
\t \t \t var video = document.currentScript.parentElement; 
 
\t \t \t video.volume = 0.1; \t \t 
 
\t \t \t var lastSong = null; 
 
\t \t \t var selection = null; 
 
\t \t \t var playlist = ["main/songn.mp4", "main/songl.mp4", "/main/songt.mp4", "/main/songf.mp4"]; // List of Songs 
 
\t \t \t var video = document.getElementById("vid"); 
 
\t \t \t video.autoplay=true; 
 
\t \t \t video.addEventListener("ended", selectRandom); 
 

 
\t \t \t function selectRandom(){ 
 
\t \t \t \t while(selection == lastSong){ 
 
\t \t \t \t \t selection = Math.floor(Math.random() * playlist.length); 
 
\t \t \t \t } 
 
\t \t \t \t lastSong = selection; 
 
\t \t \t \t video.src = playlist[selection]; 
 

 
\t \t \t } 
 

 
\t \t \t selectRandom(); 
 
\t \t \t video.play(); 
 

 
\t \t </script> 
 
</video>

+0

この回答を確認してくださいhttps://stackoverflow.com/questions/10668399/play-next-video-in-an-array-on-ended – Lalit

+0

それは役に立ちません。 – newcool

+0

'var video = document.currentScript.parentElement;'このような方法で 'currentScript'が使用されたことは一度もありません。 – zer00ne

答えて

2

は、あなただけのvideoタグからloopパラメータを削除する必要があります終了時に新しい曲をロードしてループを処理します)。

ところで、var video = document.getElementById("vid");<video>タグを参照してください。最初の宣言よりも短く、よりクリーンです。

+1

は、[specs](https://html.spec.whatwg.org/multipage/media.html#end-playback)からの引用の恩恵を受ける可能性があります:* "メディア要素は、次の場合に再生を終了したと言われています。 。] - 現在の再生位置はメディアリソースの終わりであり、[...] - メディア要素にはループ属性が指定されていません。 "* – Kaiido

+0

@Kalidoさんは文書の改善のために感謝します – PRMoureu

+0

ありがとうございます。それは完璧に動作します<3 – newcool

関連する問題