【问题标题】:custom NSView with video player is not showing (MacOS)未显示带有视频播放器的自定义 NSView (MacOS)
【发布时间】:2020-12-11 08:08:34
【问题描述】:

我创建了一个嵌入了 AVPlayer 的自定义 NSView,但它根本没有显示

这是我的标题:

@interface VideoPlayer : NSView
- (void)loadVideo;
@end

实施:

@implementation VideoPlayer

AVPlayer *avPlayer;
AVPlayerLayer* playerLayer;

- (instancetype)initWithFrame:(NSRect)rect {
    self = [super initWithFrame:rect];
    [self setWantsLayer:YES];
    [self setNeedsDisplay:YES];
    self.hidden = NO;
    return self;
}

-(void) loadVideo {
    [avPlayer pause];
    avPlayer = nil;
    playerLayer = nil;

    NSURL *videoURL = [NSURL fileURLWithPath:@"file://~/Documents/tests.mp4"];
    //same error with a valid url
    //NSURL *videoURL = [NSURL URLWithString:@"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4"];

    avPlayer = [AVPlayer playerWithURL:videoURL];
    playerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
    playerLayer.frame = self.frame;//or your desired CGRect
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    //playerLayer.masksToBounds = YES;
    [self.layer addSublayer:playerLayer];

    [avPlayer play];
}

@end

这就是我创建视图的方式:

view = [[VideoPlayer alloc] initWithFrame:NSMakeRect(0, 0, 500, 500)];
view.hidden = false;
[view loadVideo];
[parent_view addSubview:view];

我尝试了多种组合,但结果始终相同...应用程序上没有呈现任何内容。

另一方面,如果我像这样覆盖视图中的 drawRect 方法:

- (void)drawRect:(NSRect)dirtyRect {
         [super drawRect:dirtyRect];
         [[NSColor whiteColor] setFill];
         NSRectFill(dirtyRect);
         [super drawRect:dirtyRect];
}

我得到一个白色矩形,它应该显示我的视频,这是预期的。所以我假设我遗漏了其他东西,但大多数示例都使用 UIView 这不是我目前的选择(我正在处理一些特定的约束)

有什么想法吗?

谢谢

更新:

这终于对我有用了:

-(void) loadVideo {

    AVPlayer *avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"file://localhost/Users/xxxxxxxx/Documents/xxxxxxx.mp4"]];
    AVPlayerView *playerView = [[AVPlayerView alloc] initWithFrame:self.bounds];
    [playerView setPlayer:avPlayer];
    [self addSubview:playerView];
    [playerView.player play];
}

【问题讨论】:

标签: objective-c macos avplayer nsview avkit


【解决方案1】:

为了让事情更清楚一点,下面的源代码将为音频文件创建一个基本的 AVPlayer。请注意,AVPlayerView 和 AVPlayer 分别需要添加 AVKit 和 AVFoundation 框架。该代码使用自动引用计数,可以在终端中运行或在 Xcode 中使用,方法是替换 main.m 和 AppDelegate 并删除 info.plist 中的主 nib。要在终端中运行,请将代码复制/粘贴到名为 avplayer.m 的文件中并使用 cmdLine,如下所示。 Apple 文档声明它只能在 MacOS 上运行。

/*
To run in Terminal: clang avplayer.m -fobjc-arc -framework Cocoa -framework AVKit -framework AVFoundation -o avplayer && ./avplayer
*/

#import <Cocoa/Cocoa.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
 NSWindow *window;
 AVPlayerView *playerView;
}
- (void) openAction;
- (void) buildMenu;
- (void) buildWindow;
@end

@implementation AppDelegate

- (void) openAction {
 NSOpenPanel *op = [NSOpenPanel openPanel];
 if ( [op runModal] == NSModalResponseOK ) {
 AVPlayer *player = [AVPlayer playerWithURL:[op URL]];
 [playerView setPlayer:player];
 [window setTitleWithRepresentedFilename:[[op URL]lastPathComponent]];
 }
}

- (void) buildMenu {
 NSMenu *menubar = [NSMenu new];
 NSMenuItem *menuBarItem = [NSMenuItem new] ;
 [menubar addItem:menuBarItem];
 [NSApp setMainMenu:menubar];
 NSMenu *appMenu = [NSMenu new];
 NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
 [appMenu addItem:quitMenuItem];
 [menuBarItem setSubmenu:appMenu];
}

- (void) buildWindow {

#define _wndW  500
#define _wndH  400

window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH ) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable backing: NSBackingStoreBuffered defer: NO];

[window center];
[window setTitle: @"Test window"];
[window makeKeyAndOrderFront: nil];

// **** AVPlayerView **** //
playerView = [[AVPlayerView alloc] initWithFrame:NSMakeRect( 0, 60, _wndW, _wndH - 60)];
[[window contentView] addSubview:playerView];

// **** Open Button **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 20, 125, 30 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Open audio file"];
[myBtn setAction: @selector (openAction)];
[[window contentView] addSubview: myBtn];

// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAutoresizingMask: NSViewMinXMargin];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}

- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWindow];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
 return YES;
}

@end

int main () {
 NSApplication *application = [NSApplication sharedApplication];
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 [application setDelegate:appDelegate];
 [application run];
 return 0;
}


【讨论】:

  • 刚刚发现上面的demo也会打开一个.mp4的视频文件。
【解决方案2】:
  1. 确保将 AVKit 框架添加到您的项目中。
  2. AVKit 框架中有一个名为 AVPlayerView.h 的头文件
  3. 那么你需要做的就是调用它:
// **** AVPlayer ***** //
AVPlayerView *playerView = [[AVPlayerView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW, _wndH - 100)];
[[window contentView] addSubview:playerView];

我有一个程序化演示,如果你无法让它工作,我可以发布它。

【讨论】:

  • @DiegoGomez 所以你用AVPlayerView替换了AVPlayerLayer,对吧?