1

我正在尝试刷新视频播放完毕的 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
4

1 回答 1

1

首次创建 playerLayer 时,将其保存在 mvar(成员变量)中。让我们假设它被称为 _myPlayerLayer。在您的下一个:和上一个:方法中,执行以下操作:

[_myPlayer removeFromSuperLayer];

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

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

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

于 2015-03-29T05:04:27.853 回答