AVPlayerViewController 播放后退出全屏

Posted

技术标签:

【中文标题】AVPlayerViewController 播放后退出全屏【英文标题】:AVPlayerViewController exit fullscreen once played 【发布时间】:2016-11-03 12:39:40 【问题描述】:

嗨,我用 AVPlayerViewController 替换了 MPMoviePlayerController,因为 MPMoviePlayerController 已被弃用。 我快到了,但有一个问题。我的电影从视图中的视图开始。播放全屏时,我希望它在播放完毕后跳回 NO 全屏。但我不知道怎么做。这是我的代码:

- (void)viewDidLoad 

// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"movie" withExtension:@"m4v"];

// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];

// create a player view controller
self.controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];


// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(0,25, 750, 422);

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:player];

使用 MPMoviePlayer 它曾经使用此代码:

    - (void) playerPlaybackDidFinish:(NSNotification*)notification
// movie finished playing
[moviePlayerController setFullscreen:NO];

我需要用什么代码替换它??

-(void)itemDidFinishPlaying:(NSNotification *) notification 
// Will be called when AVPlayer finishes playing playerItem
 ???????????

谢谢,梅格

【问题讨论】:

可能重复? ***.com/questions/27380264/… 【参考方案1】:

#ios 10 和更高版本以及 Swift 4.2 此代码正在运行。

在您的播放器初始化方法中编写此代码

if #available(iOS 11.0, *) 
      self.playerVC?.exitsFullScreenWhenPlaybackEnds = true
 

NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC?.player!.currentItem)

这是你的通知委托

func playerItemDidReachEnd(note:NSNotification)
     print("finished")
     dismissViewControllerAnimated(true, completion: nil)
 

【讨论】:

以上是关于AVPlayerViewController 播放后退出全屏的主要内容,如果未能解决你的问题,请参考以下文章

AVPlayerViewController 播放后退出全屏

AVPlayerViewController 播放速度的自定义控制

更改 AVPlayerViewController 的播放控制按钮的颜色

如何在解雇后停止播放 AVPlayerViewController 作为 UIViewRepresentable?

AVPlayerViewController 拒绝加载/播放视频文件/流

AVPlayerViewController 不播放本地视频