Why won't my HTML5 audio loop?

本文探讨了网页音频循环播放的问题,并针对不同浏览器(如Chrome、IE9、Firefox)进行了具体分析,指出Firefox不支持音频循环播放的实现。同时提供了一种使用JavaScript检查浏览器是否支持音频循环的方法,并展示了如何在不支持的浏览器中实现音频循环播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

 

 

The site is up at: http://ajf.me/stuff/eva at the time of writing. The source code is this:

<!DOCTYPE html> 
<htmlstyle="height:100%;"> 
<head> 
<title>eva</title> 
</head> 
<bodystyle="background-color: black;background-image:url('eva.png');background-repeat: no-repeat;background-position: center center;height:100%;margin:0px;padding:0px;"> 
<audioautoplayloop> 
    <sourcesrc="eva.mp3"type="audio/mpeg"/> 
    <sourcesrc="eva.ogg"type="audio/ogg"/> 
    <sourcesrc="eva.wav"type="audio/wav"/> 
</audio> 
</body> 
</html> 

The audio plays fine in Chrome, IE9, and Firefox, but does not loop in the latter. The audio file can't be malformed, as it was produced by Audacity. Is there any other explanation for why it doesn't loop?

回答:

in Chrome, IE9  You can just do this

<audio autoplay loop>

 

Firefox has not yet implemented loop. I would check that you have the newest version of Firefox, but I believe this is still the case. You can check whether or not it is supported with:

if(typeofnewAudio().loop =='boolean') 

If that evaluates to true, then loop is implemented in the browser. If false, then it is not. Add that to your javascript, put an id tag on your audio and use that if statement to check for loop.

if!(typeofnewAudio().loop =='boolean'){ 
    audioToLoop = document.getElementById('audio_id_here'); 
    audioToLoop.addEventListener('ended',function(){ 
        this.currentTime =0; 
        this.play(); 
    },false); 
} 

Then it should loop even in unsupported browsers。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值