我有一个可以正常播放视频的 AVPlayer 对象,但是
- (void)playerItemDidReachEnd:(NSNotification *)notification
方法永远不会被调用。不确定我做错了什么,因为我完全遵循了 Apple 的文档。这是我的代码:
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[self.playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];//This works okay
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerItem];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[self.playerLayer setFrame:[[[self playerView] layer] bounds]];
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.playerLayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
[self.playerLayer setHidden:NO];
[[[self playerView] layer] addSublayer:self.playerLayer];