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

时间:2015-03-27 20:30:58

标签: objective-c macos cocoa avplayer avplayerlayer

我想在macintosh app上播放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

1 个答案:

答案 0 :(得分:1)

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

[self.view addSubView:containerView];

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