我正在使用 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];
}
关于ios - AVPlayer 播放完毕退出全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380264/