WKWebView不适用于AVAudioSession的类别

时间:2017-02-03 11:22:50

标签: ios uiwebview wkwebview avaudiosession

通过使用选项AVAudioSessionCategoryPlayback将应用程序的AVAudioSession类别设置为mixWithOthers,将使UIWebView中的视频中的声音与第三方背景音乐(例如Spotify)混合。

但是一旦切换到WKWebView,它根本不起作用。代码示例:

let audioSession = AVAudioSession.sharedInstance()
do {
    try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
    try audioSession.setActive(true)
} catch {
    print(error)
}

/*
 // Mixed background music & video sound in web works with UIWebView
 let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
 self.view.addSubview(webview)
 webview.loadRequest(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))
*/

// Mixed background music & video sound in web do NOT work with WKWebView
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
self.view.addSubview(webview)
webview.load(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))

// end of viewDidLoad

以上代码在Xcode 8.2.1,iOS 10.2.1上进行了测试

任何想法或解决方法都会非常有用。

非常感谢。

0 个答案:

没有答案