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