简体   繁体   中英

Background audio playback in UIWebView

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?

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM