2013-06-26 69 views
0

在Mac上,我想使用AVPlayer在我的Qt 4.8應用程序中播放視頻,因爲它具有Phonon::VideoPlayer沒有的功能。如何將AVPlayer添加到QWidget?

如何將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]; 

當我播放我的視頻時,我聽到它,但我沒有看到它。任何想法我做錯了什麼?

回答

0

而不是使用winId(),使用QMacCocoaViewContainer,但QMacCocoaViewContainer有一個錯誤,這將導致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];