Recently due to new product requirements, the need to open the H5 page on the mobile side, and the ability to play audio automatically, during the development process encountered the following problems:
1.IOS device cannot respond to "Loadeddata" event
Mode 1
var audio = new Audio (SRC);
Audio.addeventlistener ("Loadeddata", function () {
audio.play ();
Start playback status processing
});
Mode 2
var audio = new Audio (SRC);
Audio.play ();
After testing, the above mode 1 does not play in the iOS device, mode 2 can play normally, the process of processing audio loading over effect will encounter problems, do not know when the audio load is complete, and start playback, the solution is as follows:
var playerhandle = setinterval (function () {
if (Audio.currenttime > 0 &&!isnan (audio.duration)) {
// Has started playing, processing the corresponding logic
clearinterval (playerhandle);}
}, 0)
It is also important to note that if you need to control the playback progress, the length of the audio audio.duration must start playing, that is, Audio.currenttime > 0 o'clock gets, otherwise it will not be accurate.
2.h5 page loading finished can not be automatically played, set AutoPlay properties, audio tag add click event, and so on, because the mobile side webview have security restrictions, prohibit autoplay and other uncontrolled behavior, must be triggered by the user touch, To play, and finally the problem is solved by the mobile partner: The client has set up a security-related property to implement audio AutoPlay.