2

If I give MPMoviePlayerViewController a bad video URL to play, like so:

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

Is there a way to be notified that the video did not download?

I tried both of the following, but was not notified in either case:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateChanged) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Steve
  • 53,375
  • 33
  • 96
  • 141
  • This used to work in iOS 7 but stopped working in iOS 8. I have an open bug with Apple but they have done nothing about it. I ended up using a timer which is a terrible design but the only solution that works. – Kevin May 07 '15 at 12:17
  • Yeah, using iOS 8.1 and my MPMoviePlayer.url is getting 404 error, but it's not triggering the [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; – Lombas Jul 12 '16 at 20:22

1 Answers1

4

first of all check if URL is reachable using 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];
      });
 });

its mention is this answer link

or

[[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;
  }
}

or add a custom function for check the request timeout ..

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

if you are not receiving any Notification in your code
please check this answer for further reference .link

Community
  • 1
  • 1
rahul_send89
  • 943
  • 8
  • 19
  • Thanks, and great answer. In my case, we have a situation where we have 5 servers behind a load-balancer. Occasionally a few of those servers will return 404's, while the others return 200's. If I attempt to fetch the manifest/video (HLS) prior to the video player doing it, then it is not a guarantee that the next request by the player will succeed. Obviously we are trying to fix the server-side issue, but it would be ideal for the client to handle the situation gracefully as well. I never seem receive a notification of the playback ending... – Steve Oct 11 '14 at 18:09
  • you can add a request timeout .. using a custom function .. or use something similar to this .. [link](http://stackoverflow.com/a/5869448/1721884) or [link](http://stackoverflow.com/a/7659735/1721884) – rahul_send89 Oct 11 '14 at 18:18
  • @Steve : if the above answer didn't work for you . try using `ffmpeg` or `AVFoundation`. will try it my self before changing my answer .. try edit your question by explaining the backend problem .. – rahul_send89 Oct 11 '14 at 20:12
  • The timeout timer is a good suggestion. I will go that route a see how that goes. Thanks. – Steve Oct 12 '14 at 18:59