如何在 AVPlayerViewController 中添加完成按钮?

How to adding done button in AVPlayerViewController?

我正在使用 AVPlayerViewController,无法显示 Done 按钮,也无法关闭播放器。也许你可以帮助我。这是我在 objective-c

中的代码
UIView *view = self.view;

NSURL *fileURL = [NSURL URLWithString: _detailData[0]];

AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

playerViewController.player = [AVPlayer playerWithURL:fileURL];

self.avPlayerViewcontroller = playerViewController;

[self resizePlayerToViewSize];
[self dismissViewControllerAnimated:YES completion:nil];
[view addSubview:playerViewController.view];

[playerViewController.player play];

view.autoresizesSubviews = TRUE;

需要帮助的人,非常感谢。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); // set your own position
[view addSubview:button]; // or you can add [playerViewController.view addSubview:button];

-(void)aMethod:(UIButton *)button {

 // Remove playerViewController.view
}

感谢您的回答。我已经找到了:)

我们可以使用这个,完成按钮可以自动显示。

NSURL *fileURL = [NSURL URLWithString: _detailData[0]];

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

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

// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;