ios - AVPlayer 播放完毕退出全屏

标签 ios objective-c avplayer

我正在使用 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/

相关文章:

ios - 仅适用于 iPhone x 的 AVPlayer 全屏问题

objective-c - AVPlayer 在模拟器上可以播放,但在真实设备上却无法播放

ios - 内存泄漏: AVplayerViewController

ios - 将 16 位颜色范围内的值转换为 UIColor

iphone - 崩溃日志中我的应用名称旁边的 "jettisoned"表示什么?

ios - 在 AppStore 更新之间持久解析用户

ios - 有没有办法改变 Apple Maps 中的披露按钮的作用?

javascript - PhoneGap 构建插件无法运行

ios - SWIFT-IOS : modify a value using NSUserDefaults

iphone - 为什么所有人都建议在 iphone 中使用 PNG 而不是 GIF、JPG 图像?