我正在使用AVPlayer
显示一些视频流(具有固定长度)。问题是内容播放完毕后,播放器(如果用户已将其设置为全屏显示)仍保持全屏显示。
在内容播放完毕后,有什么方法可以使播放器立即进入最小化状态?
最佳答案
为了补充longilong的答案,
您可以使用itemDidFinishPlaying函数中的dismissViewControllerAnimated关闭视频播放器。完成后,您还应该记住删除创建的观察者。
-(void)startPlaybackForItemWithURL:(NSURL*)url {
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(itemDidFinishPlaying:)
name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Begin playback
[player play]
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[self dismissViewControllerAnimated:YES completion:Nil]
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:Nil];
}