playerItemDidReachEnd移到错误的ViewController

问题描述 投票:0回答:1

我的应用程序有一个起始页和一个第二页,其中包含播放视频的按钮。视频播放完毕后,将使用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
objective-c xcode avplayer
1个回答
0
投票

如果我正确理解您的代码,则您的“第二页”为ViewController

您正在通过AVPlayerViewController添加[self.view addSubview:playerViewController.view];,因此您需要通过调用删除此视图[playerViewController.view removeFromSuperview]

[self dismissViewControllerAnimated:NO completion:nil];之后,您将整个ViewController而不是AVPlayerViewController全部消除。

© www.soinside.com 2019 - 2024. All rights reserved.