Objective-C和React-Native,播放器按钮无响应

时间:2017-06-28 12:59:17

标签: objective-c react-native

我正在尝试在React-Native应用程序中实现Vimeo视频播放器。

视频播放并停止播放,但我无法退出视频播放器。播放/暂停按钮响应很好。但是视频上方栏上的按钮都没有响应。

我对Objective-C很新,我觉得NSNotificationCenter defaultCenter出了问题,因为它没有发送任何消息。

可以告诉我这段代码有什么问题吗?

   @implementation PVCPVideoPlayerManager


    id<PVCPVideoPlayerManagerDelegate> __videoDelegate = nil;


    RCT_EXPORT_MODULE()


    +(void)setDelegate:(id<PVCPVideoPlayerManagerDelegate>)delegate {__videoDelegate = delegate;}


    RCT_EXPORT_METHOD(playFullScreen:(NSString *)url) {
      dispatch_async(dispatch_get_main_queue(), ^{

        [[YTVimeoExtractor sharedExtractor]fetchVideoWithVimeoURL:url withReferer:nil completionHandler:^(YTVimeoVideo * _Nullable video, NSError * _Nullable error) {

          if (video) {
            MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[video highestQualityStreamURL]];

            UIView* view = [__videoDelegate viewControllerToPlayFullScreen].view;

            player.shouldAutoplay = YES;
            player.controlStyle = MPMovieControlStyleEmbedded;
            player.view.frame = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
            player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [player setMovieSourceType:MPMovieSourceTypeFile];
            [player prepareToPlay];

            [view addSubview:player.view];

            [player setFullscreen:YES animated:YES];

            [PVCPOrientationManager lockToLandscapeLeft];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];


          }else{
            UIAlertView *alertView = [[UIAlertView alloc]init];
            alertView.title = error.localizedDescription;
            alertView.message = error.localizedFailureReason;
            [alertView addButtonWithTitle:@"OK"];
            alertView.delegate = self;
            [alertView show];

          }
        }];
      });
    }


-(void)moviePlayerPlaybackDidFinish:(NSNotification *)notification {
  dispatch_async(dispatch_get_main_queue(), ^{
    MPMoviePlayerController *player = notification.object;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerWillExitFullscreenNotification object:player];
    [PVCPOrientationManager lockToPortrait];
    [player setFullscreen:NO animated:YES];
    [player stop];
    [player.view removeFromSuperview];
  });
}

@end

1 个答案:

答案 0 :(得分:0)

PVCPVideoPlayerManager是否实施了方法moviePlayerPlaybackDidFinish?我在代码中没有看到它。

此外,您应该在模块加载时注册一次通知,并在模块销毁时注销。例如:

- (id)init
{
    if ((self = [super init])) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)moviePayerPlaybackDidFinish:(NSNotification *)notification
{
    NSLog(@"Got a notification");
}
相关问题