Setting AVAudioSession Category has no effect on sound from WKWebView

╄→尐↘猪︶ㄣ 提交于 2019-12-04 23:09:48

问题


I can not seem to override the AVAudioSession category or port when audio is being played from a WKWebView. The same code works perfectly when using a normal UIWebView.

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

I've also tried activating the sharedInstance like this, but it did not help:

[session setActive: YES error: nil];

The above code has no effect on audio coming from a WKWebView. I did find some reports on Twitter that iOS 8.1 is mixing WKWebView audio with background app audio, but I couldn't find the source for that. See this twitter thread for reference: https://twitter.com/marcoarment/status/530881842900373504


回答1:


So apparently WKWebView runs in a separate process from your app. Which probably means, it has its own AudioSession separate from your app's AudioSession as well. That's why changes to your app's AudioSession won't affect the webView. And also why it works with UIWebView (no separate process). At least that's what I gathered so far...

The solution for me was to allow my app's AudioSession to mix with others:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord 
                                 withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                       error:&error];

Of course this has other implications as well. Your app's audio will be mixed with all other apps' audio, not just your webview's.



来源:https://stackoverflow.com/questions/27152211/setting-avaudiosession-category-has-no-effect-on-sound-from-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!