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

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");
}

最新更新