Background audio playback in UIWebView

▼魔方 西西 提交于 2020-01-14 04:34:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!