0

我有一個視頻播放器,可以從某個給定網址加載視頻並播放。每當給定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; 
     } 
+0

你能告訴我一些你的代碼?尤其是關於何時加載網址。 –

+0

我編輯了這個問題。謝謝eugene – hridayesh

+0

只是一個快速的:你什麼時候添加設置通知?在創建/重新創建self.playerController實例之前還是之後? –

回答

0

處理錯誤代碼: -

由於您已添加此notif ication

[[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: -

+0

問題是這個回調沒有被調用。 – hridayesh

+0

然後你必須以這種方式描述問題,你說這個「 - 每當給定url的資源無效並且服務器返回帶有錯誤響應的錯誤代碼時,我無法獲得相同的任何回調」, – Vizllx

+0

U還沒有提到你的通知沒有解僱。 – Vizllx

相關問題