Search code examples
xcodemacosavplayeravplayerlayer

Refresh and set new item AVPlayer class


I'm trying to refresh NSView in which video finishes playing. I'd like to after I click button next or previous refresh view in which video is played. Actually I see new view overlapping old vindow. I've tried to do this using removeFromSuperView but it removes NSVindow. How to solve this?

MY CODE:

@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

Solution

  • When you first create your playerLayer, save it in a mvar (member variable). Let us suppose it is called _myPlayerLayer. In your next: and previous: methods, do this:

    [_myPlayer removeFromSuperLayer];
    

    and then create a new instance of a Player layer, assign it to _myPlayer, and add it as a sublayer of the view's layer as you did before.

    Notice that this code will not affect the view at all.

    I don't know why you are calling setSubviews: - that line looks wrong.