刷新并设置新项目 AVPlayer class

Refresh and set new item AVPlayer class

我正在尝试刷新视频播放完毕的 NSView。我想在单击下一个按钮或上一个播放视频的刷新视图后。实际上,我看到新视图与旧窗口重叠。我尝试使用 removeFromSuperView 来执行此操作,但它删除了 NSVindow。如何解决?

我的代码:

@interface AppDelegate ()
{
    AVPlayer *player;
}

@property (weak) IBOutlet NSWindow *window;
@end



@implementation AppDelegate

@synthesize playerView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {


    NSURL *url = [[NSBundle mainBundle] URLForResource:@"pieniadz" withExtension:@"3gp"];
    player = [AVPlayer playerWithURL:url];

     AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [self.playerView.layer addSublayer:playerLayer];
    playerLayer.frame = self.playerView.layer.bounds;
    playerLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

   // [player play];

    // Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}
- (IBAction)play:(id)sender {
    [player play];

}

- (IBAction)stop:(id)sender {
    [player pause];

}
- (IBAction)previous:(id)sender {
    [player pause];
    [self.playerView removeFromSuperview];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"pieniadz" withExtension:@"3gp"];
    player = [AVPlayer playerWithURL:url];
    [self.playerView.layer removeAllAnimations];
    //[self.playerView setNeedsDisplay:YES];
    //[self.playerView.layer removeAllAnimations];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [self.playerView.layer addSublayer:playerLayer];

    // [self.playerView.layer setNeedsDisplay];
    playerLayer.frame = self.playerView.layer.bounds;
    playerLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}


- (IBAction)next:(id)sender {
    [player pause];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Fatality" withExtension:@"mp4"];
    [self.playerView setSubviews:[NSArray array]];
    player = [AVPlayer playerWithURL:url];
    //[self.playerView setNeedsDisplay:YES];
    [self.playerView.layer removeAllAnimations];
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [self.playerView.layer addSublayer:playerLayer];

   // [self.playerView.layer setNeedsDisplay];
        playerLayer.frame = self.playerView.layer.bounds;
        playerLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

}
@end

当您第一次创建 playerLayer 时,将其保存在 mvar(成员变量)中。让我们假设它被称为 _myPlayerLayer。在你的 next: 和 previous: 方法中,这样做:

[_myPlayer removeFromSuperLayer];

然后创建一个 Player 图层的新实例,将其分配给 _myPlayer,并像之前一样将其添加为视图图层的子图层。

请注意,此代码根本不会影响视图。

我不知道你为什么调用 setSubviews: - 那一行看起来不对。