带有视频播放器的自定义NSView未显示MacOS

问题描述

我已经创建了一个嵌入了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,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];
}

解决方法

为使情况更清楚一些,以下源代码将为音频文件创建一个基本的AVPlayer。请注意,AVPlayerView和AVPlayer分别需要添加AVKit和AVFoundation框架。该代码使用自动引用计数,可以通过替换main.m和AppDelegate并删除info.plist中的主笔尖在Terminal中运行或在Xcode中使用。要在终端中运行,请将代码复制/粘贴到名为avplayer.m的文件中,并使用cmdLine,如下所示。苹果文档指出它只能在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,_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,_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;
}


,
  1. 确保将AVKit框架添加到项目中。
  2. AVKit框架中有一个名为AVPlayerView.h的标头
  3. 然后,要查看播放器,您需要做的就是调用它:
// **** AVPlayer ***** //
AVPlayerView *playerView = [[AVPlayerView alloc] initWithFrame:NSMakeRect( 0,_wndH - 100)];
[[window contentView] addSubview:playerView];

我有一个程序化演示,如果您无法使它正常工作,我可以发布该演示。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...