Problem with Obj C / AVFoundation in QMacCocoaViewContainer



  • Hi there,

    I'm trying to create a very lightweight media player, for OS X only, because the multimedia module uses way too much cpu time. My problem is that, although I have sound, I'm not seeing the picture. I'm sure I'm making a fundamental mistake somewhere but I can't seem to find the answer in the Apple Dev documentation. Here's what I've got so far:

    avPlayer.h:

    #define AVPLAYER_H
    
    #include <QtCore>
    #include <QMacCocoaViewContainer>
    
    #ifdef __OBJC__
    # define ADD_COCOA_NATIVE_REF(CocoaClass) \
        @class CocoaClass; \
        typedef CocoaClass *Native##CocoaClass##Ref
    #else /* __OBJC__ */
    # define ADD_COCOA_NATIVE_REF(CocoaClass) typedef void *Native##CocoaClass##Ref
    #endif /* __OBJC__ */
    
    ADD_COCOA_NATIVE_REF(xplayer);
    
    class avPlayer:QWidget
    {
        Q_OBJECT
    public:
        avPlayer(QWidget *parent=NULL);
        ~avPlayer();
        void loadVideo(QString mf);
    private:
        NativexplayerRef m_video;
        QMacCocoaViewContainer *m_container;
    };
    
    #endif // AVPLAYER_H
    

    avPlayer.mm:

    #include <Cocoa/Cocoa.h>
    #include <AVFoundation/AVFoundation.h>
    
    @interface xplayer:NSView
    {
        AVPlayer *player;
        AVPlayerItem *playerItem;
        AVPlayerLayer *playerLayer;
        AVAsset *asset;
        CALayer *layer;
    }
        @property (nonatomic, retain) AVPlayer *player;
        @property (nonatomic, retain) AVPlayerItem *playerItem;
        @property (nonatomic, retain) AVPlayerLayer *playerLayer;
        @property (nonatomic,retain) AVAsset *asset;
        @property (nonatomic, retain) CALayer *layer;
        -(id)initWithFrame:(NSRect)frame;
        -(void)loadFile:(NSString *)filename;
        -(void)play;
        -(void)stop;
    @end
    
    
    @implementation xplayer
    @synthesize player, playerItem, playerLayer;
    
    -(id)initWithFrame:(NSRect)frame
    {
        self=[super initWithFrame:frame];
        if (self)
        {
            layer=[[CALayer alloc]init];
            [self setHidden:NO];
            [self setNeedsDisplay:YES];
            [self setWantsLayer:YES];
            [self makeBackingLayer];
            [self setLayer:layer];
            [layer setBackgroundColor:CGColorCreateGenericRGB(1.0, 0.0, 1.0, 1.0)];
        }
        return self;
    }
    
    -(void) loadFile:(NSString*)filename
    {
            NSURL *f=[NSURL fileURLWithPath:filename];
            player=[AVPlayer playerWithURL:f ];
            playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
            [self.layer addSublayer:playerLayer];
    
            [self.layer display];
    }
    
    - (void) play {
        [self.player play];
    }
    
    - (void) stop {
        [self.player pause];
    }
    
    @end
    
    avPlayer::avPlayer(QWidget *parent):QWidget(parent)
    {
        m_video=[[xplayer alloc]init];
        m_container=new QMacCocoaViewContainer(0,this);
        m_container->setGeometry(parent->rect());
        m_container->setVisible(true);
        m_container->setCocoaView(m_video);
        m_container->setAttribute(Qt::WA_NativeWindow);
        m_container->setAutoFillBackground(true);
    }
    
    avPlayer::~avPlayer()
    {
    
    }
    
    void avPlayer::loadVideo(QString mf)
    {
        NSString *t=mf.toNSString();
        [m_video loadFile:t];
       // for testing play the video immediately after loading...
        [m_video play];
    }
    

    Any help or pointers would be gratefully received as my Objective C skills are virtually non existent.

    TIA
    Ziggy


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.