Home:ALL Converter>WKWebView not playing audio

WKWebView not playing audio

Ask Time:2019-07-01T18:23:23         Author:Harsh Srivastava

Json Formatter

I am setting a WKWebView but, here the web code contains an audio code which is not working in WKWebView. The device I am using is iPad Mini of iOS version 12.3.1

here is the backend code:-

<audio id="alertSound" preload="auto">
  <source src="sound/music-box.mp3" type="audio/mpeg" />
  <source src="sound/music-box.wav" type="audio/x-wav" />
</audio>

And this is my implementation iOS side

    func initWebView() {

    let preference = WKPreferences()
    preference.javaScriptEnabled = true

    let configuration = WKWebViewConfiguration()
    configuration.preferences = preference
    configuration.allowsInlineMediaPlayback = true
    configuration.allowsAirPlayForMediaPlayback = true
    configuration.allowsPictureInPictureMediaPlayback = true
    configuration.mediaTypesRequiringUserActionForPlayback = []


    self.wkWebView = WKWebView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), configuration: configuration)
    self.wkWebView.contentMode = .scaleAspectFit
    self.wkWebView.allowsLinkPreview = true
    self.wkWebView.allowsBackForwardNavigationGestures = false
    self.webContentView.addSubview(self.wkWebView)
}

The expected result is need auto playing audio when the user is being called in the application using WKWebView

Author:Harsh Srivastava,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56833974/wkwebview-not-playing-audio
Kuldeep Chander :

Your iOS code is right, only need to add \"autoplay=true\" attribute in audio tag, then audio will start automatically play. Please see the below code.\n\n <audio autoplay=\"true\">\n <source src=\"music-box.mp3\" type=\"audio/mpeg\" />\n </audio>\n",
2019-07-01T11:59:31
Salmon Yang :

I think it is not change, iOS need user click if need play audio. Cant auto play audio.",
2019-07-01T10:35:08
yy