【发布时间】:2018-02-04 10:50:33
【问题描述】:
我正在使用 YouTube iOS Helper Library 播放 WKWebView 中的视频,我希望即使按下 HOME 或锁定键也能继续播放音频。
功能选项卡如下所示:
我还在AppDelegate中导入了import AVFoundation
在func applicationDidBecomeActive(_ application: UIApplication) 里面我有这个代码:
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
// Handle setCategory failure
print(error)
}
}
当应用程序进入后台音频停止,我什至无法从控制中心恢复它。
【问题讨论】:
-
很确定这只是 YouTube 的事情。如果您尝试使用其他网站(例如 Vimeo)会发生什么情况?
-
我使用的是
youtube-player-ios-helper而不是网站 -
我也在尝试解决同样的问题,在 WKWebView 中我自己的页面使用 WebAudio API 来产生声音。每次应用程序进入后台并稍后重新进入前台时,WebAudio 调用都会失败。我尝试在 applicationWillEnterForeground 或 applicationDidBecomeActive 中调用 AVAudioSession 的 setActive。到目前为止没有任何效果。
标签: ios swift swift3 uiwebview