itemFailedToPlayToEnd - MPMoviePlayerController -

2019-03-24 13:11发布

问题:

Running into a weird issue while updating my app for iOS 7. For iOS 6, there is no problem and the videos are always loaded. However that is not the case here.

I have a scrollview which displays playlistItems - and once you click on the playlistItem, a MPMoviePlayerController is created to show the playlist item (video).

Here is the method that I believe is causing issues:

- (void) play:(NSInteger)itemId withAutostart:(BOOL)autostart {

  // remember whether it is in fullscreen or not to restore for the next playlist item
  self.playerInFullscreen = self.player.isFullscreen;

  if (player != nil) {
    [self.player setFullscreen:NO animated:NO];
    [self stopListeningPlaybackFinishedEvents];
    [player stop];
    [self startListeningPlaybackFinishedEvents];
  }

  PlaylistItem * pi = [dbHelper getPlaylistItem:itemId];
  NSURL *movieURL = [pi getMovieUrl];

  if (DELEGATE.useSSL) {

    NSURLCredential *credential = [[NSURLCredential alloc]
                                 initWithUser: DELEGATE.username
                                 password: DELEGATE.password
                                 persistence: NSURLCredentialPersistenceForSession];

    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                           initWithHost: [movieURL host]
                                           port: 80
                                           protocol: [movieURL scheme]
                                           realm: [movieURL host]
                                           authenticationMethod: NSURLAuthenticationMethodHTTPBasic];

    [[NSURLCredentialStorage sharedCredentialStorage]
     setDefaultCredential: credential
     forProtectionSpace: protectionSpace];

    [protectionSpace release];
    [credential release];
  }


  MPMoviePlayerController * temp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [temp prepareToPlay];

  self.player =  temp;
  [temp release];

    player.shouldAutoplay = autostart;
  player.view.frame = movieViewContainer.bounds; 
  [movieViewContainer addSubview:player.view];

    NSLog(@"movie added to subview");

  [player setFullscreen:self.playerInFullscreen animated:NO];
  [self.view setNeedsDisplay];
}

The movie is getting loaded to the MovieContainerView.

- (void)playlistItemSelected:(NSInteger)itemId withAutostart:(BOOL) autostart {
  for(UIView *subview in [thumbsScrollView subviews]) {
    if ([subview isKindOfClass:[PlaylistItemView class]] == YES ) {

      PlaylistItemView *piv = ((PlaylistItemView *) subview);
      [piv setCurrent: piv.playlistItem._id == itemId];

      if (piv.playlistItem._id == itemId) {
        [self ensurePlaylistItemViewVisible:piv];
      }
    }
  }
  [[movieViewContainer subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
  self.currentPlaylistItem = [dbHelper getPlaylistItem:itemId];
  [self updateMetadataArea:itemId];

  if ([_currentPlaylistItem.type isEqual:VIDEO_TYPE]) {
    self.zoomButtonOut.hidden = YES;
    self.zoomButtonIn.hidden  = YES;

    [self play:itemId withAutostart:autostart];
  }
  else {
     [self.player pause];
    _zoomButtonIn.alpha = ZOOM_BUTTON_ALPHA;
    _zoomButtonOut.alpha = ZOOM_BUTTON_ALPHA;
    [self showPDFandImage:_currentPlaylistItem];
  }
  [self scrollViewDidEndDecelerating:nil];
 }

It's weird that this works for iOS 6 but not iOS 7

Here are the NSLog errors when a video does not load/play: <Error>: CGImageCreate: invalid image size: 0 x 0. and: _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }

回答1:

It would be two issues

  1. Problem with url

    a) If it is fetching from api's you have to write ,[NSUrl urlwithString:"your url string"];

    // your url string does not be youtube url string.

    b) If it is from bundle path : [NSUrl fileWithPath:"Your path"];

  2. Problem with Source type.

    Set scouce type to any one , if it not works set to other type viceversa

    yourPlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    

    or

    yourPlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
    


回答2:

I'm having the same issue on my iOS 7 build. May I ask if you have any white spaces and numbers in your video's URL?

i.e. my url was something like "video title 5.2.5.mov" and when I escaped the string using the NSUTF8StringEncoding, the result was "video%20title%205.2.5.mov" and it didn't work.

I tried changing the movie url to video_title_5_2_5.mov" and it loaded fine

hope this advice helps a little



回答3:

A tip for those implementing local file caches: include file extensions

I have a local download cache in one of my applications that does not differentiate on filetype [for better or worse], resulting in files without types associated with them. When I serve the file from the server directly into the movie controller, the file plays fine, however when it is stored locally into the path: [AppDocumentsDirectory]/123 (without an extension) it fails to play. When I stored the file as 123.mp4 it succeeded.



回答4:

I ended up testing the app on an actual ipod - and it worked. The url Im init with the mpmoviecontroller didn't have any spaces that i needed to escape. Hope this helps... the videos dont work on simulator which was throwing me off



回答5:

I had this same problem and it was fixed by setting

[MPMoviePlayerViewController.moviePlayer setShouldAutoplay:YES]

The problem I was seeing was that the movie player would push onto the navigation controller, but right when it appeared it would disappear and I would get the _itemFailedToPlayToEnd error in the log



回答6:

I was having the same issue . First check that file from where you want to play that video does exist or not.Then Play . In my case my file was in document directory .so

    BOOL hasVideoFile= [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",[self getDocumentDirectory] videoFileName]];
    if (!hasVideoFile) {
        //No video found
        NSLog(@"Video file doesn't exists in this directory");
    }