【问题标题】:Refresh and set new item AVPlayer class刷新并设置新项目 AVPlayer 类
【发布时间】:2025-12-07 06:30:02
【问题描述】:

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

【问题讨论】:

    标签: xcode macos avplayer avplayerlayer


    【解决方案1】:

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

    [_myPlayer removeFromSuperLayer];
    

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

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

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

    【讨论】: