html5 mobile对象,javascript - Reusing HTML5 Audio Object in Mobile Safari - Stack Overflow

在iOS Safari上,一个关于在用户交互后播放短音频的问题被提出。尽管音频文件在首次点击时播放,但之后无法再次播放。尝试通过监听'ended'事件并重新加载或设置currentTime为0来解决,但遇到了错误或无效的结果。开发者寻求一种方法,能在多个页面刷新之间实现音频文件的缓存和无延迟播放。

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

I want to play a short (less than 1s) audio file in response to user input on my web app running in Mobile Safari on the iPad with minimum latency between event and audio playback. The playback can be triggered multiple times between page reloads, therefore I want to cache the audio file.

The following plays the file on the first click but after than nothing happens:

var audio = new Audio("ack.mp3");

$("#button").click(function(e) {

e.preventDefault();

audio.play();

}

If I add an event listener to the "ended" event which reloads the file I can get two playbacks from the same object and then silence:

var audio = new Audio("ack.mp3");

audio.addEventListener('ended', function() {

audio.load();

}, false);

$("#button").click(function(e) {

e.preventDefault();

audio.play();

}

If I manually set the currentTime attribute to 0 like this:

var audio = new Audio("ack.mp3");

audio.addEventListener('ended', function() {

audio.currentTime=0;

}, false);

$("#button").click(function(e) {

e.preventDefault();

audio.play();

}

I get the following error in the error console:

INDEX_SIZE_ERR: DOM Exception 1: Index or size was negative, or greater than the allowed value.

Any ideas on how to make this work? Thanks in advance

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值