2

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

3 Answers3

0

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.

 <audio autoplay="true">
        <source src="music-box.mp3" type="audio/mpeg" />
 </audio>
-1

I think it is not change, iOS need user click if need play audio. Cant auto play audio.

-1
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
webConfig.allowsInlineMediaPlayback = YES;
if (@available(iOS 10.0, *)) {
    webConfig.mediaTypesRequiringUserActionForPlayback = NO;
}
  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer). Kind Regards. – Elletlar Nov 12 '20 at 10:23
  • The OP's code already contains this configuration. – aleclarson Dec 23 '22 at 02:57