我有一个视频播放器,可以从某个给定网址加载视频并播放。每当给定url处的资源无效并且服务器返回带有错误响应的错误代码时,我都无法获得相同的回调。我订阅了以下通知。ios MPMoviePlayerController在播放视频时检测未找到网址
[[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网址。每当视频失败时,我都想回到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];
代码段,同时设置URL(从viewdidappear,通知也在这里加入,该代码是在开始)与MPMoviePlayerViewController错误响应
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;
}
你能告诉我一些你的代码?尤其是关于何时加载网址。 –
我编辑了这个问题。谢谢eugene – hridayesh
只是一个快速的:你什么时候添加设置通知?在创建/重新创建self.playerController实例之前还是之后? –