OGeek|极客世界-中国程序员成长平台

标题: ios - MPMoviePlayerController 错误的 URL 错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:32
标题: ios - MPMoviePlayerController 错误的 URL 错误

如果我给 MPMoviePlayerViewController 一个错误的视频 URL 来播放,像这样:

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

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

我尝试了以下两种方法,但在任何一种情况下都没有收到通知:

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



Best Answer-推荐答案


首先使用 Rechability 检查 URL 是否可访问。

 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 selectorselector(playbackDidFinish name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


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

或者添加一个自定义函数来检查请求超时..

[self performSelectorselector(checkTimeout withObject:theMovie afterDelay:15];

如果您的代码中没有收到任何 通知
请检查此答案以供进一步引用。 link

关于ios - MPMoviePlayerController 错误的 URL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184103/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4