AVPlayer 播放声音但视图未显示在 mac 应用程序中

AVPlayer plays sound but view is not shown in mac application

我想在 macintosh 应用程序的 AVPlayer 中播放本地电影。我创建了简单的代码,但电影不想显示播放的电影。它播放电影的声音,但看不到任何视图。我错过了什么?

这是我第一次玩简单的应用程序。

我的代码:

@implementation MasterViewController

    - (void)viewDidLoad {
    [super viewDidLoad];

    NSView *containerView = [[NSView alloc] initWithFrame:CGRectMake(0.0f, 0, 390.0f, 400.0f)];


NSString *videoPath = [[NSBundle mainBundle] pathForResource: @"Fatality.mp4" ofType:nil];
NSLog(@"%@", videoPath);



NSURL *fileURL = [NSURL fileURLWithPath:videoPath];
self.player = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];

[self.player play];

}

@end

和 AppDelegate:

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (nonatomic,strong) IBOutlet MasterViewController *masterViewController;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    self.masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];

    // 2. Add the view controller to the Window's content view
    [self.window.contentView addSubview:self.masterViewController.view];
    self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;


}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end

您必须将 containerView 添加为主视图控制器视图的子视图

[self.view addSubView:containerView];

调试时我经常做的一个技巧是将我感兴趣的视图的背景颜色设置为明亮的颜色(红色、黄色、青色....),然后确保该视图显示在我的位置期待它。