【发布时间】:2019-02-16 10:35:21
【问题描述】:
我在尝试播放背景音频时遇到一个奇怪的错误
我做了以下事情:
能力✅
框架 ✅
我的代码:
var player = AVPlayer()
var playerViewController = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
UIApplication.shared.beginReceivingRemoteControlEvents()
addPlayerNotifications()
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
fatalError(error.localizedDescription)
}
}
addPlayerNotifications():
func addPlayerNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidPlayToEnd), name: .AVPlayerItemDidPlayToEndTime, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
但是,这是一个标签栏应用程序,当切换到相应的部分时,我会因以下错误而崩溃:
线程 1:致命错误:操作无法完成。 (OSStatus 错误 -50。)在这一行:
fatalError(error.localizedDescription)
更奇怪,
此代码适用于:
模拟器✅
实体版 iPhone X (iOS 12.4.1) ❌
在另一个没有 Tabbar 的 Xcode 项目中 ✅
这是一个带有 2 个视图控制器的简单标签栏。我真的不知道我做错了什么
【问题讨论】: