Ios AVPlayerViewController在播放后退出全屏
您好,我用avplayervewcontroller替换了MPMoviePlayerController,因为MPMoviePlayerController已被弃用。 我快到了,但有一个问题。我的电影以视图中的视图开始。当全屏播放时,我希望它在播放结束后跳回无全屏状态。但我不知道怎么做。这是我的代码:Ios AVPlayerViewController在播放后退出全屏,ios,objective-c,avplayer,avplayerviewcontroller,Ios,Objective C,Avplayer,Avplayerviewcontroller,您好,我用avplayervewcontroller替换了MPMoviePlayerController,因为MPMoviePlayerController已被弃用。 我快到了,但有一个问题。我的电影以视图中的视图开始。当全屏播放时,我希望它在播放结束后跳回无全屏状态。但我不知道怎么做。这是我的代码: - (void)viewDidLoad { // grab a local URL to our video NSURL *videoURL = [[NSBundle mainBundle]UR
- (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];
}
- (void) playerPlaybackDidFinish:(NSNotification*)notification{
// movie finished playing
[moviePlayerController setFullscreen:NO];
}
使用MPMoviePlayer时,它通常使用以下代码:
- (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];
}
- (void) playerPlaybackDidFinish:(NSNotification*)notification{
// movie finished playing
[moviePlayerController setFullscreen:NO];
}
我需要用什么代码替换它??
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
???????????}
谢谢,Meg#iOS 10及更高版本和Swift4.2
此代码正在运行
在player init方法中编写此代码
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)
}
可能重复?