问题
My app allows users to access a service that plays audio books via the web. I am using a UIWebView to handle this.
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.
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?
回答1:
First, enable your app for background audio playback in your Info.plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
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)
}
来源:https://stackoverflow.com/questions/38482544/background-audio-playback-in-uiwebview