我有一个视频播放器,可以从一些给定的网址加载视频并播放。每当给定URL的资源无效且服务器返回错误响应的错误代码时,我都无法获得相同的回调。我订阅了以下通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDurationAvailable:) name:MPMovieDurationAvailableNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willChangePlayingMovie:) name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:nil];
我正在使用hls视频播放视频。我的媒体有hls和mp4网址。每当hls视频失败时,我都想回到mp4。
编辑: 只是为了澄清,没有得到回调意味着通知没有解雇。抱歉混淆。
viewdidload上的代码段
self.playerController = [[MPMoviePlayerController alloc] init];
self.playerController.shouldAutoplay = YES;
self.playerController.controlStyle = MPMovieControlStyleNone;
[self.playerController.view setFrame:self.playerView.bounds];
self.playerController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.playerController.view translatesAutoresizingMaskIntoConstraints];
[self.playerView addSubview:self.playerController.view];
self.playerController.view.userInteractionEnabled = NO;
self.playerController.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
self.playerController.backgroundView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
//To hide top and bottom Bar
self.playerController.view.backgroundColor = [UIColor blackColor];
self.playerController.backgroundView.backgroundColor = [UIColor blackColor];
设置网址时(来自viewdidappear,此处也添加了通知,代码就是开头)
NSURL *fileUrl;
NSString *fileExtension;
fileUrl = self.content.media.hlsUrl;
if ([Reachability reachabilityForInternetConnection].isReachable) {
[self.playerController setContentURL:fileUrl];
BOOL isFirstTimeUpdate = [[NSUserDefaults standardUserDefaults] boolForKey:@"IS_FIRST_TIME_UPDATE"];
if(isFirstTimeUpdate == NO){
[self.playerController pause];
}
else{
[self.playerController play];
}
[self.activityIndicator startAnimating];
}else{
self.errorLabel.text = kZErNoInternet;
self.errorView.hidden = NO;
}
答案 0 :(得分:0)
使用MPMoviePlayerViewController处理错误响应错误代码: -
您已添加此通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerController];
(void)movieFinished:(NSNotification *)notification
{
NSDictionary *notificationUserInfo = [notification userInfo];
NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
MPMovieFinishReason reason = [resultValue intValue];
if (reason == MPMovieFinishReasonPlaybackError)
{
NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"];
if (mediaPlayerError)
{
NSLog(@"playback failed with error description: %@", [mediaPlayerError localizedDescription]);
}
else
{
NSLog(@"playback failed without any given reason");
}
}
}
刚从这个link获得参考: -