Search code examples
objective-cxcodemacoscocoalibvlc

VLCKit: VLCMediaPlayerDelegate in a Cocoa Application


I'm trying to develop a Cocoa Application for Mac OSX 10.10 that implements some video streaming in VLCKit. Now:

  1. I've compiled the .framework library and I've imported it in Xcode.
  2. I've added a Custom View in my Main.storyboard and set it to a VLCVideoView

The View

  1. In my ViewController.h i've implemented the VLCMediaPlayerDelegate to receive notification from the player

Here's my code:

viewController.h

#import <Cocoa/Cocoa.h>
#import <VLCKit/VLCKit.h>

@interface ViewController : NSViewController<VLCMediaPlayerDelegate>

@property (weak) IBOutlet VLCVideoView *_vlcVideoView;

//delegates
- (void)mediaPlayerTimeChanged:(NSNotification *)aNotification;

@end

viewController.m

#import "ViewController.h"

@implementation ViewController
{
    VLCMediaPlayer *player;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [player setDelegate:self];

    [self._vlcVideoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
    self._vlcVideoView.fillScreen = YES;

    player = [[VLCMediaPlayer alloc] initWithVideoView:self._vlcVideoView];

    NSURL *url = [NSURL URLWithString:@"http://MyRemoteUrl.com/video.mp4"];

    VLCMedia *movie = [VLCMedia mediaWithURL:url];
    [player setMedia:movie];
    [player play];
}

- (void)mediaPlayerTimeChanged:(NSNotification *)aNotification
{
    //Here I want to retrieve the current video position.
}

@end

The video start and play correctly. However I can't get the delegate to work. Where I am wrong?

Here are my questions:

  1. How can I setup the delegate to receive notifications about the current player time?
  2. How can I read the NSNotification? (I'm not really used to Obj-C)

Thank you in advance for any answer!


Solution

  • I've managed it!

    1. How can I setup the delegate to receive notifications about the current player time? I had to add an observer to NSNotificationCenter.

    Here's the code:

    - (void)viewDidLoad
    {
       [super viewDidLoad];
    
       [player setDelegate:self];
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerTimeChanged:) name:VLCMediaPlayerTimeChanged object:nil];
    }
    
    1. How can I read the NSNotification? I had to retrieve the VLCMediaPlayer object inside the notification.

    Code:

    - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification
    {
       VLCMediaPlayer *player = [aNotification object];
       VLCTime *currentTime = player.time;
    }