Xcode 刷新并设置新项目AVPlayer类

Xcode 刷新并设置新项目AVPlayer类,xcode,macos,avplayer,avplayerlayer,Xcode,Macos,Avplayer,Avplayerlayer,我正在尝试刷新视频播放结束时的NSView。我想在单击按钮“下一步”或“上一步”后刷新播放视频的视图。事实上,我看到新的观点和旧的观点重叠。我曾尝试使用removeFromSuperView执行此操作,但它删除了NSVindow。如何解决这个问题 我的代码: @interface AppDelegate () { AVPlayer *player; } @property (weak) IBOutlet NSWindow *window; @end @implementation

我正在尝试刷新视频播放结束时的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];
然后创建一个播放器层的新实例,将其分配给_myPlayer,并像以前一样将其添加为视图层的子层

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

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