HTML5-----Audio Tags

Source: Internet
Author: User

In the current company to work need to do a HTML5 page, up and down can swipe, on the page need music, the default start music playback, click the music tag after the music stop. Later in the project development, encountered the problem of performance optimization, so I recommend you do not use the audio tag directly in the page in the future

Example: <audio src= "/h5/a/bg.mp3" AutoPlay loop data-src= "/h5/a/bg.mp3" id= "Musicall" ></audio>

This is not very good, the page pre-parsing time can be played, and finally with JS in the page after the completion of the loading to add

Window.onload = function () {
var music = document.getElementById (' U-globalaudio ');
var audio = document.createelement ("audio");
var music_img = document.getelementsbytagname (' img ') [0];
Audio.setattribute (' src ', '/h5/a/bg.mp3 ');
Audio.setattribute (' data-src ', '/h5/a/bg.mp3 ');
Audio.setattribute (' Loop ', ');
Audio.setattribute (' AutoPlay ', ');
Audio.setattribute (' id ', ' music1 ');
Music.appendchild (audio);
var OnOFF = true;
var audio = document.getElementById (' Music1 ');
Music_img.onclick = function () {
if (audio!==null) {
Detects if playback is paused. audio.paused return False.<span style= "font-family:arial, Helvetica, sans-serif when player plays" > Return true</span> when player is paused

if (!audio.paused)
{
Audio.pause ();//This is the pause//audio.play ();//This is the play
OnOFF = false;
}else{
Audio.play ();
OnOFF = true;
}
}
}
SetTimeout ("document.getElementById (' Musicall '). src= '/h5/a/bg.mp3 ';", 3000);



}

HTML5-----Audio Tags

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.