HTML5 音频重新开始 [英] HTML5 audio start over

查看:41
本文介绍了HTML5 音频重新开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有

var audio = new Audio("click.ogg")

我在需要时播放点击声

audio.play()

然而,有时用户速度太快以至于浏览器根本不播放音频(可能是在播放之前的 play 请求时).此问题是否与预加载有关?

However, sometimes user is so fast that a browser does not play the audio at all (probably when still playing a previous play request). Is this issue related to preload?

如何强制浏览器停止播放并重新开始?HTML5 音频组件中没有stop,只有pause,对吗?这里可以使用什么解决方法?

How can I force a browser to stop playing and start over? There is no stop, just pause in HTML5 audio component, correct? What workaround can be used here?

更新 - 附加说明:

我有多个类似复选框的 div 元素和一个 touchend 事件.当此类事件被触发时,元素在视觉上发生变化,播放声音并相应地设置内部变量.如果用户慢慢点击这些元素,一切都很好.如果快速点击,声音通常会完全跳过...

I have multiple checkbox-like div elements with a touchend event. When such event is triggered, the elements visually change, a sound is played and an internal variable is set accordingly. If user tap on these elements slowly, everything works nicely. If tap fast, the sound is often completely skipped...

推荐答案

你可以试试这个代码:

audio.currentTime = 0;
audio.play();

看起来这是最简单的解决方案.

It looks like this is the simplest solution.

这篇关于HTML5 音频重新开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆