VLCKit: VLCMediaPlayerDelegate in Cocoa app
I am trying to develop a Cocoa app for Mac OSX 10.10 that implements video streaming in VLCKit. Now:
- I put together a .framework library and I imported it into Xcode.
- I added Custom View to my Main.storyboard and set it to VLCVideoView
- In my ViewController.h, I have implemented VLCMediaPlayerDelegate to receive notifications 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 starts and plays correctly. However, I can't seem to get the delegate to work. Where am I going wrong?
Here are my questions:
- How do I configure a delegate to receive notifications about the current player time?
- How can I read NSNotification? (I'm not very used to Obj-C)
Thanks in advance for any answer!
+3
source to share
1 answer
I did it!
- How do I configure a delegate to receive notifications about the current player time? I had to add 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];
}
- How can I read NSNotification? I needed to restore the VLCMediaPlayer object inside the notification.
code:
- (void)mediaPlayerTimeChanged:(NSNotification *)aNotification
{
VLCMediaPlayer *player = [aNotification object];
VLCTime *currentTime = player.time;
}
+3
source to share