The implementation of automatic playback of HTML5 page audio and video under the app, html5app
Currently, most H5 pages support playing background music and videos. How can we achieve automatic playback?
Pure H5 pages cannot be automatically played on mobile phones. Most mobile browsers disable the autoplay function of video and audio, many mobile browsers do not support the first JavaScript call of the play method for playback (only when you manually pause the playback and then use the code for play ).
This is mainly used to prevent unnecessary automatic playback from wasting traffic.
The following code is used to enable playback after the first touch and automatic playback under the app.
Copy XML/HTML Code to clipboard
- Function autoPlayMusic (){
- /* Automatically play the music effect to solve the problem of automatic playback by the browser or APP */
- Function musicInBrowserHandler (){
- MusicPlay (true );
- Document. body. removeEventListener ('touchstart', musicInBrowserHandler );
- }
- Document. body. addEventListener ('touchstart', musicInBrowserHandler );
- /* Automatically play the music effect to solve the automatic playback problem */
- Function musicInWeixinHandler (){
- MusicPlay (true );
- Document. addEventListener ("WeixinJSBridgeReady", function (){
- MusicPlay (true );
- }, False );
- Document. removeEventListener ('domainloaded', musicInWeixinHandler );
- }
- Document. addEventListener ('domainloaded', musicInWeixinHandler );
- }
- Function musicPlay (isPlay ){
- Var media = document. getElementById ('mymusic ');
- If (isPlay & media. paused ){
- Media. play ();
- }
- If (! IsPlay &&! Media. paused ){
- Media. pause ();
- }
- }
The above section describes the implementation of HTML5 page audio and video playback and automatic playback under the app. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!