【问题标题】:AVPlayerViewController exit fullscreen once playedAVPlayerViewController 播放后退出全屏
【发布时间】: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
 ???????????}

谢谢,梅格

【问题讨论】:

标签: ios objective-c avplayer avplayerviewcontroller


【解决方案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)
 }

【讨论】:

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