【问题标题】:AVPlayer exit fullscreen on finish playingAVPlayer 播放完退出全屏
【发布时间】:2014-12-09 13:37:35
【问题描述】:

我正在使用AVPlayer 来显示一些视频流(具有固定长度)。问题是内容完成后,播放器(如果用户已将其设置为全屏)仍保持全屏状态。

有什么方法可以让播放器在播放完内容后立即进入最小化状态?

【问题讨论】:

标签: ios objective-c avplayer


【解决方案1】:

为了补充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];

}

【讨论】:

    【解决方案2】:

    它适用于#iOS 10 版及更高版本

        # declare your player and 
        var player = AVPlayer()
        var playerVC = AVPlayerViewController()
    
    
        let item = AVPlayerItem(url: module.fileURL!)
        DispatchQueue.main.async(execute: {
            self.playerVC.view.backgroundColor = UIColor.clear
            self.player.replaceCurrentItem(with: item)
            try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            self.playerVC.player = self.player
    
            self.player.play()
    
            self.playerVC.showsPlaybackControls = true
            if #available(iOS 11.0, *) {
                self.playerVC.exitsFullScreenWhenPlaybackEnds = true
            }
            self.playerVC.view.frame = self.videoView.bounds
            NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC.player!.currentItem)
            self.videoView.addSubview((self.playerVC.view)!)
    
    })
    

    收听视频文件的结尾

      @objc func playerItemDidReachEnd(notification: Notification?) {
        print("Finishing")
        dismiss(animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案3】:

      收听视频文件的结尾并在最后关闭/调整大小/重新初始化视图

      -(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
      
      }
      

      【讨论】:

        猜你喜欢
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2015-09-06
        • 2015-05-05
        相关资源
        最近更新 更多