Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with Obj C / AVFoundation in QMacCocoaViewContainer
Forum Updated to NodeBB v4.3 + New Features

Problem with Obj C / AVFoundation in QMacCocoaViewContainer

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 710 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ziggx
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved