【问题标题】:How do I add an AVPlayer to a QWidget?如何将 AVPlayer 添加到 QWidget?
【发布时间】:2013-06-26 18:41:56
【问题描述】:

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

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

    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];

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

【问题讨论】:

    标签: macos cocoa qt4 avplayer qt4.8


    【解决方案1】:

    使用QMacCocoaViewContainer而不是使用winId(),但是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];
    

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多