0

在 Mac 上,我想使用 AVPlayer 在我的 Qt 4.8 应用程序中播放视频,因为它具有Phonon::VideoPlayer没有的功能。

如何将 AVPlayerLayer 添加到 aQWidget以使其可见?我正在执行以下操作:

    pimpl->player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:videoFile]];
    pimpl->player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
    pimpl->player.rate = 0;

    pimpl->playerLayer = [[AVPlayerLayer playerLayerWithPlayer:pimpl->player] retain];
    [pimpl->playerLayer setFrame: CGRectMake(0, 0, rect().width(), rect().height())];

    NSView* view = (NSView*)winId();
    [[view layer] addSublayer:pimpl->playerLayer];

当我播放我的视频时,我会听到它,但我看不到它。知道我做错了什么吗?

4

1 回答 1

0

不是使用winId(),而是使用QMacCocoaViewContainer,但是QMacCocoaViewContainer有一个bug会导致NSView在Qt 4.8中不显示,除非setAttribute(Qt::WA_NativeWindow); 已设置。

同样,NSView 可能没有图层,因此需要创建图层。

    [pimpl->view setWantsLayer:YES];
    [pimpl->view makeBackingLayer];

所以总的来说它看起来像:

    pimpl->view = [[NSView alloc] initWithFrame: CGRectMake(0, 0, w, h)];
    [pimpl->view setHidden:NO];
    [pimpl->view setNeedsDisplay:YES];
    [pimpl->view setWantsLayer:YES];
    [pimpl->view makeBackingLayer];

    viewFrame = new QMacCocoaViewContainer(NULL, this);
    viewFrame->setGeometry(rect());
    viewFrame->setVisible(true);
    viewFrame->setCocoaView(pimpl->view);

    NSString* videoFile = [NSString stringWithCString:filename.toUtf8().data() encoding:NSUTF8StringEncoding];

    pimpl->player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:videoFile]];
    pimpl->player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
    pimpl->player.rate = 0;

    pimpl->playerLayer = [[AVPlayerLayer playerLayerWithPlayer:pimpl->player] retain];
    [pimpl->playerLayer setFrame: CGRectMake(0, 0, w, h)];
    [pimpl->playerLayer setHidden: NO];

    CALayer* layer = [pimpl->view layer];
    [layer addSublayer:pimpl->playerLayer];
于 2013-06-26T22:13:41.857 回答