VLCKit:Cocoa应用程序中的VLCMediaPlayerDelegate

时间:2014-11-06 00:06:22

标签: objective-c xcode macos cocoa libvlc

我正在尝试为Mac OSX 10.10开发一个Cocoa应用程序,它在VLCKit中实现了一些视频流。 现在:

  1. 我已编译 .framework 库,并已将其导入Xcode。
  2. 我在Main.storyboard中添加了自定义视图,并将其设置为 VLCVideoView
  3. The View

    1. 在我的ViewController.h中,我已实施 VLCMediaPlayerDelegate 以接收播放器的通知
    2. 这是我的代码:

      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
      

      视频正常启动和播放。但是,我无法让代表工作。 哪里错了?

      以下是我的问题:

      1. 如何设置委托以接收有关当前播放器时间的通知?
      2. 我如何阅读NSNotification? (我并不习惯Obj-C)
      3. 提前感谢您的回答!

1 个答案:

答案 0 :(得分:4)

我已经成功了!

  1. 如何设置委托以接收有关当前播放器时间的通知?我必须在 NSNotificationCenter 中添加观察者
  2. 以下是代码:

    - (void)viewDidLoad
    {
       [super viewDidLoad];
    
       [player setDelegate:self];
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerTimeChanged:) name:VLCMediaPlayerTimeChanged object:nil];
    }
    
    1. 如何阅读NSNotification?我必须在通知中检索VLCMediaPlayer对象。
    2. 代码:

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