我的应用程序有一个起始页和一个第二页,其中包含播放视频的按钮。视频播放完毕后,将使用playerItemDidReachEnd关闭AVPlayer。但是由于某种原因,当播放器被解雇时,它显示的是起始页,而不是第二页。我怎样才能显示第二页?
#import "ViewController.h"
@import AVKit;
@import AVFoundation;
@interface ViewController ()
@property(nonatomic, readonly) AVPlayerItem *currentItem;
@end
@implementation ViewController
AVPlayerViewController *playerViewController;
- (void)viewDidLoad {
[super viewDidLoad];
playerViewController = [[AVPlayerViewController alloc] init];
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)playVideo:(id)sender {
NSURL *url = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
UIScreen *mainScreen = [UIScreen mainScreen];
CGRect viewRect = mainScreen.bounds;
playerViewController.view.frame = viewRect;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
/* When the player item has played to its end time we'll dismiss the controller */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerViewController.player.currentItem];
}
@end
如果我正确理解您的代码,则您的“第二页”为ViewController
。
您正在通过AVPlayerViewController
添加[self.view addSubview:playerViewController.view];
,因此您需要通过调用删除此视图[playerViewController.view removeFromSuperview]
。
在[self dismissViewControllerAnimated:NO completion:nil];
之后,您将整个ViewController
而不是AVPlayerViewController
全部消除。