MPMoviePlayerController Bad Url错误

时间:2014-10-03 17:50:22

标签: ios

如果我给MPMoviePlayerViewController播放一个糟糕的视频网址,就像这样:

[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://badurl"]];

有没有办法通知视频没有下载?

我尝试了以下两种情况,但在任何一种情况下均未收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateChanged) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

1 个答案:

答案 0 :(得分:4)

首先检查是否可以使用Rechability访问网址。

 NSURL *myURL = [NSURL urlWithString: @"http://example.com"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
 [request setHTTPMethod: @"HEAD"];
 dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^{
      NSURLResponse *response;
      NSError *error;
      NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
      BOOL reachable;

      if (myData) {
            // we are probably reachable, check the response
            reachable=YES;
      } else {
            // we are probably not reachable, check the error:
            reachable=NO;
      }

      // now call ourselves back on the main thread
      dispatch_async( dispatch_get_main_queue(), ^{
            [self setReachability: reachable];
      });
 });

提到的是这个答案link

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


- (void) playbackDidFinish:(NSNotification*)notification{
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
    case MPMovieFinishReasonPlaybackEnded:
        NSLog(@"Playback Ended");         
        break;
    case MPMovieFinishReasonPlaybackError:
        NSLog(@"Playback Error"); //// this include Bad URL
        break;
    case MPMovieFinishReasonUserExited:
        NSLog(@"User Exited");
        break;
    default:
        break;
  }
}

或添加自定义函数以检查请求超时..

[self performSelector:@selector(checkTimeout:) withObject:theMovie afterDelay:15];

如果您的代码中没有收到任何Notification 请查看此答案以供进一步参考。link