由于我在tick方法中遇到崩溃,因为我可以看到从setuptimer启动了一个长时间运行的循环,所以我正在缓冲来自webservice的音频并应用播放/暂停并继续。
func setupTimer(){
NotificationCenter.default.addObserver(self, selector: #selector(self.didPlayToEnd), name: .AVPlayerItemDidPlayToEndTime, object: nil)
timer = Timer(timeInterval: 0.001, target: self, selector: #selector(myclass.tick), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoopMode.commonModes)
}
@objc func tick(){
if((avPlayer.currentTime().seconds == 0.0) && (isPaused == false)){ //crasing in this line because of loop
seekLoadingLabel.alpha = 1
}else{
seekLoadingLabel.alpha = 0
}
if(isPaused == false){
if(avPlayer.rate == 0){
avPlayer.play()
//seekLoadingLabel.alpha = 1
}else{
//seekLoadingLabel.alpha = 0
}
}
崩溃报告
Crashed: com.apple.main-thread 0 Myapp 0x1001e5b84 MyDetailTableViewCell.tick() + 283 (CPObjectDetailTableViewCell.swift:283) 1 Myapp 0x1001e5bdc @objc MyTableViewCell.tick() + 4337966044 (<compiler-generated>:4337966044) 2 Foundation 0x1a729c028 __NSFireTimer + 64 3 CoreFoundation 0x1a6e3103c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 28 4 CoreFoundation 0x1a6e30d78 __CFRunLoopDoTimer + 880 5 CoreFoundation 0x1a6e30448 __CFRunLoopDoTimers + 276 6 CoreFoundation 0x1a6e2b584 __CFRunLoopRun + 1920 7 CoreFoundation 0x1a6e2aadc CFRunLoopRunSpecific + 464 8 GraphicsServices 0x1b0dcb328 GSEventRunModal + 104 9 UIKitCore 0x1aaf3863c UIApplicationMain + 1936 10 MyApp 0x1001a7a60 main + 25 (AppDelegate.swift:25) 11 libdyld.dylib 0x1a6cb4360 start + 4
我已经通过使用可选链接检查avPlayer对象是否为空值来解决此问题。
if let avPlayerObj = avPlayer {
print("Contains a value! It is \(avPlayerObj)! execute tick()")
}else {
print("Doesn’t contain a avPlayerObj or nil")
}