0

HTML5 ビデオ タグで 2 つのビデオを連続して再生するにはどうすればよいですか?

Google Chrome では、次のコードは最初のイントロ ビデオのみを再生します。

<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script>

var i = 0;
var sources = ['1.mp4', '2.mp4'];
videoElement.addEventListener('ended', function(){
   videoElement.src = sources[(++i)%sources.length];
   videoElement.load();
   videoElement.play();
}, true);

</script>

</head>
<body>
<video id="videoElement" width="640" height="360" autoplay="autoplay">
    <source src="intro.mp4" type="video/mp4"></source>
</video>

<body>
<html>
4

2 に答える 2

4

ブラウザはJavaScriptコードで「videoElementisnotdefined」というエラーを発生させる必要があります。IDを直接使用するのではなく、DOMからビデオ要素を取得する必要があります。コードを次のように変更してください

$(document).ready(function() {
    //place code inside jQuery ready event handler 
    //to ensure videoElement is available
    var i = 0;
    var sources = ['1.mp4', '2.mp4'];
    $('#videoElement').bind('ended', function() {
        //'this' is the DOM video element
        this.src = sources[i++ % sources.length];
        this.load();
        this.play();
    });
});
于 2011-11-08T16:25:59.757 に答える