objective-c - AVPlayer 播放声音,但在 Mac 应用程序中不显示 View

标签 objective-c macos cocoa avplayer avplayerlayer

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

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

我的代码:

@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添加为主视图 Controller View 的 subview

[self.view addSubView:containerView];

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

关于objective-c - AVPlayer 播放声音,但在 Mac 应用程序中不显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309694/

相关文章:

ios - 与 iOS 中的 setter/getter 相关的奇怪的 EXC_BAD_ACCESS 崩溃

ios - 将数据从自定义表格 View 单元格传递到 UIView Controller

ios - 使用 Apple Script 将多个文件从多个文件夹移动到一个

iphone - NSTask waitUntilExit 在越狱 iOS 上挂起应用程序

objective-c - 在哪里定义了像 NSControlKeyMask 这样的标识符

objective-c - 带有 iOS 8 SDK 的 Xcode 6 无法在 Instruments 中运行 UI 自动化

macos - 如何使用 Applescript 获取菜单项列表?

macos - 如何在 Mac OSX Leopard 上安装 JDK 1.6?

objective-c - 在 Cocoa 应用程序中使用异常处理与 NSError

objective-c - 具有内容范围的 Cocoa HTTP PUT