3 回答
TA贡献1780条经验 获得超1个赞
您需要设置应用程序功能后台模式(音频和AirPlay),并将AVAudioSession类别设置为AVAudioSessionCategoryPlayback并将其设置为活动状态
从Xcode 8.2.1•Swift 3.0.2开始
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
Xcode 10•Swift 4.2
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
TA贡献1869条经验 获得超4个赞
Xcode 10.2.1 Swift 4
请在您的AppDelegate中添加以下代码
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
return true
}
注意:-请根据需要配置选项。例如在播放视频文件时停止背景音频
options: [.allowAirPlay, .defaultToSpeaker]
并且不要忘记在后台模式下启用音频和播放
- 3 回答
- 0 关注
- 616 浏览
添加回答
举报