A music solution for Seamless loop playback

Source: Internet
Author: User

Scene:

In order to save page resources, it is often necessary to play a small audio loop, usually by adding the loop property on the Audio tab, but unfortunately, this property does not guarantee a seamless loop (gapless looping) playback, obviously feeling the middle of the pause.

Solution:

Use the audio tag's Web API to provide methods and properties for looping, as follows

Event name Event function
Timeupdate triggered when the current playback time has changed

Property name Attribute action
CurrentTime Used to get or control the current playback time, in units of S
Duration Gets the total length of time, in S, of the media file, if it cannot be obtained, returns Nan

The main listener Timeupdate event, and then compare the playback time properties CurrentTime and audio Time-length properties duration, at the end of the time, the CurrentTime property is reset to 0, the code is as follows://Use the Web audio API
var audio_file = new Audio ('./videos/bg.mp3 ');
Audio_file.play ();
Audio_file.addeventlistener (' Timeupdate ', function () {
var buffer = 1.2;
if (This.currenttime > This.duration-buffer) {
This.currenttime = 0;
This.play ();
}}, False);
Usage Requirements:(1) music as far as possible is "fade" type, stress as far as possible not at the beginning or end position, otherwise, even if the music can be played back, it will not feel cohesion; (2) API interface can only be used for WebKit kernel, compare new browser

A music solution for Seamless loop playback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.