我有一個視頻播放器,可以從某個給定網址加載視頻並播放。每當給定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實例之前還是之後? –