UIWebView中的后台音频播放 [英] Background audio playback in UIWebView
问题描述
我的应用程序允许用户访问通过网络播放有声读物的服务.我正在使用UIWebView来处理此问题.
My app allows users to access a service that plays audio books via the web. I am using a UIWebView to handle this.
退出应用程序或使设备进入睡眠状态后,音频将停止播放.由于我只是显示Web视图而不是直接播放音频文件,因此无法使用这些方法在后台播放音频.
When the app is exited or the device is put to sleep, the audio stops playing. Since I am just displaying the web view not playing the audio file directly, I cannot use those methods to play the audio in the background.
但是,如果您通过Safari应用程序访问相同的链接,则在手机进入睡眠状态后,音频将继续播放.如何在我的应用程序中实现相同的效果?
However, if you access the same link via the Safari app, the audio keeps playing after the phone is put to sleep. How can I achieve the same effect in my app?
推荐答案
首先,在Info.plist中为您的应用启用背景音频播放:
First, enable your app for background audio playback in your Info.plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
如果您尚未这样做,请在AppDelegate或其他课程中设置您的应用以进行音频播放:
If you're not already doing so, set up your app for audio playback in your AppDelegate or other class:
import AVFoundation
// ...
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
// Handle setCategory failure
print(error)
}
这篇关于UIWebView中的后台音频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!