Setting the category AVAudioSession does not affect the sound from WKWebView

I cannot redefine the category or port of AVAudioSession when sound is played from WKWebView. The same code works fine when using a regular UIWebView.

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

I also tried activating sharedInstance like this, but this did not help:

 [session setActive: YES error: nil]; 

The above code does not affect the sound coming from WKWebView. I found some Twitter posts stating that iOS 8.1 mixes WKWebView audio with the background sound of the application, but I could not find the source of this. See this thread on twitter for reference: https://twitter.com/marcoarment/status/530881842900373504

+7
ios cordova cordova-plugins wkwebview avaudiosession
source share
1 answer

So, obviously, WKWebView starts in a separate process from your application. This probably means that it has its own AudioSession separate from your AudioSession application. This is why changes to your AudioSession application will not affect webView. And also why it works with UIWebView (without a separate process). At least what I have gathered so far ...

The solution for me was to let my AudioSession application mix with others:

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

Of course, this has other consequences. Your audio application will be mixed with the sound of all other applications, not just your web browsing.

0
source share

All Articles