gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:50:04 27 4
gpt4 key购买 nike

我想在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com