1. Inject the JS code (Swift 4) that stops the Web page from audio and video playback:
1Let pausejsstring:string =2 """3 varVideos = document.getElementsByTagName ("Video");4 function Pausevideo () {5 varLen =Videos.length6 for(varI=0; i<len;i++){7 videos[i].pause ();8 }9 }Ten varAudios = document.getElementsByTagName ("Audio"); One function Pauseaudio () { A varLen =Audios.length - for(varI=0; i<len;i++){ - audios[i].pause (); the } - } - """ -Let Pausejs:wkuserscript = Wkuserscript.init (source:pausejsstring, Injectiontime: Wkuserscriptinjectiontime.atdocumentstart, Formainframeonly:true) + -Let Usercontentctrl:wkusercontentcontroller =Wkusercontentcontroller.init () + usercontentctrl.adduserscript (Pausejs) AConf.usercontentcontroller =Usercontentctrl at -WebView = Wkwebview.init (Frame:CGRect.zero, configuration:conf)
2. Before exiting, call the JS function that pauses playback (Swift 4):
Override Func Viewdiddisappear (_ Animated:bool) { super.viewdiddisappear (animated) Pauseplay () }
/// pause playback of audio and video in a Web page func pauseplay () { mwebview.evaluatejavascript ("pausevideo ()" in } mwebview.evaluatejavascript ("pauseaudio () " inch } }
Ficow Original, reproduced please specify the source:http://www.cnblogs.com/ficow/p/7637922.html
IOS Wkwebview Stop playing audio/video after exiting