最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 video loop src change on end play function not working - Stack Overflow

programmeradmin3浏览0评论

I have a number of videos playing one by one in the same HTML5 video player

HTML

<video id="homevideo" width="100%" autoplay autobuffer>
    <source src="app/video1.mp4" type='video/mp4' />
</video>

JS

video_count = 1;
videoPlayer = document.getElementById("homevideo");
videoPlayer.addEventListener("ended", function (){
    video_count++;
    if (video_count == 4) video_count = 1;
    var nextVideo = "app/video" + video_count + ".mp4";
    videoPlayer.src = nextVideo;
    videoPlayer.play();
}, false); 

If I put an alert before playing, it's working. But without the alert it's not. Initialize the second video play:

videoPlayer.src = nextVideo;
alert("a");
videoPlayer.play();

I have a number of videos playing one by one in the same HTML5 video player

HTML

<video id="homevideo" width="100%" autoplay autobuffer>
    <source src="app/video1.mp4" type='video/mp4' />
</video>

JS

video_count = 1;
videoPlayer = document.getElementById("homevideo");
videoPlayer.addEventListener("ended", function (){
    video_count++;
    if (video_count == 4) video_count = 1;
    var nextVideo = "app/video" + video_count + ".mp4";
    videoPlayer.src = nextVideo;
    videoPlayer.play();
}, false); 

If I put an alert before playing, it's working. But without the alert it's not. Initialize the second video play:

videoPlayer.src = nextVideo;
alert("a");
videoPlayer.play();
Share Improve this question edited May 14, 2015 at 15:38 DBS 9,9844 gold badges38 silver badges58 bronze badges asked Jul 8, 2013 at 7:18 DhamuDhamu 1,7525 gold badges22 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

The html5 video element has an own eventtype onended, you dont need your own eventlistener.

Html:

<video id="homevideo" width="100%" autoplay onended="run()">
    <source src="app/video1.mp4" type='video/mp4'/>
</video>

and

Script:

video_count =1;
videoPlayer = document.getElementById("homevideo");

function run(){
        video_count++;
        if (video_count == 4) video_count = 1;
        var nextVideo = "app/video"+video_count+".mp4";
        videoPlayer.src = nextVideo;
        videoPlayer.play();
   };

PS: Keep in mind that providing only one format for your video is not enough since no single format is supported by all major browsers yet.

EDIT:

LINK
Source: w3schools

At Media Events
Events triggered by medias like videos, images and audio (applies to all HTML elements, but is most mon in media elements, like <audio>, <embed>, <img>, <object>, and <video>):

onended: Script to be run when the media has reach the end (a useful event for messages like "thanks for listening")

发布评论

评论列表(0)

  1. 暂无评论