我正在尝试刷新视频播放完毕的NSView。我想点击下一个或之前刷新视频的按钮后播放视频。实际上我看到新视图重叠旧vindow。我尝试使用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
答案 0 :(得分:1)
首次创建playerLayer时,请将其保存在mvar(成员变量)中。我们假设它被称为_myPlayerLayer。在您的下一个:和之前的:方法中,执行以下操作:
[_myPlayer removeFromSuperLayer];
然后创建一个Player层的新实例,将其分配给_myPlayer,并像以前一样将其添加为视图层的子层。
请注意,此代码根本不会影响视图。
我不知道你为什么要调用setSubviews: - 那条线看起来不对。