我想在我的应用程序中播放一个视频,我使用avplayer没有问题,但是在视频结束后,应用程序崩溃了,我得到了这个错误:
未识别的选择器发送到实例0x7f950bf6d1a0 2015-10-09 14:42:42.769拼车47925:1543415*终止应用程序由于未识别的异常'NSInvalidArgumentException',原因:‘-拼车。视图控制器playerItemDidReachEnd::无法识别的选择器发送到实例0x7f950bf6d1a0’。
我犯了什么错?
lazy var playerLayer:AVPlayerLayer = {
let player = AVPlayer(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
player.muted = true
player.allowsExternalPlayback = false
player.appliesMediaSelectionCriteriaAutomatically = false
var error:NSError?
// This is needed so it would not cut off users audio (if listening to music etc.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch var error1 as NSError {
error = error1
} catch {
fatalError()
}
if error != nil {
print(error)
}
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
playerLayer.backgroundColor = UIColor.blackColor().CGColor
player.play()
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: player.currentItem)
return playerLayer
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.layer.addSublayer(self.playerLayer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}发布于 2015-10-09 11:34:17
您正在向NSNotificationCenter注册一个观察者,并告诉观察者在收到通知时调用playerItemDidReachEnd:,但是您的代码没有实现playerItemDidReachEnd:,因此代码找不到方法并崩溃。
发布于 2015-10-09 11:40:48
“Bensarz”上面的答案是正确的。你错过了接收方法。
添加以下方法
func playerItemDidReachEnd(playerItem: AVPlayerItem) {
//Do your stuff here
}https://stackoverflow.com/questions/33037209
复制相似问题