Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  22] [ 4]  / answers: 1 / hits: 23061  / 12 Years ago, sun, october 21, 2012, 12:00:00

Having



var audio = new Audio(click.ogg)


I play the click sound when needed by



audio.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?



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?






Update - Additional note:



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...


More From » jquery

 Answers
90

The simplest solution is to just reset the audio currentTime and ensure it's playing using the play() method. Checking if the audio is playing is not necessary as subsequent play() invocations will not do anything.


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

[#82442] Friday, October 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
martin

Total Points: 405
Total Questions: 93
Total Answers: 93

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;