Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34780

MPMoviePlayerController can't play local file with error _itemFailedToPlayToEnd

I have a local iPhone-compatible MP4 video (I've both converted the video for iPhone, and I've tested uploading the video to a web server and entering its URL into mobile Safari, it plays perfectly) that I want to play using MPMoviePlayerController.

Here is my code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Welcome" ofType:@"mp4"];
NSURL *fileURL = [NSURL URLWithString:filePath];
playerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
playerController.movieSourceType = MPMovieSourceTypeFile;
playerController.backgroundView.backgroundColor = [UIColor whiteColor];

playerController.view.translatesAutoresizingMaskIntoConstraints = YES;
playerController.scalingMode = MPMovieScalingModeAspectFill;
//[playerController.view setFrame:moviePlayerContainerView.frame];
[playerController.view setFrame:CGRectMake(0, 0, 200, 200)];
[moviePlayerContainerView addSubview:playerController.view];
playerController.contentURL = fileURL;
playerController.fullscreen = YES;
[playerController prepareToPlay];
[playerController play];

fileURL points to a valid file:

(lldb) po fileURL
/var/mobile/Applications/73C97CC9-731E-4446-A53C-2A8CA30A2393/Tanisalim.app/Welcome.mp4
(lldb) p (BOOL)[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]
(BOOL) $0 = YES

When I try to run on simulator (both 6.1 and 7.1), it just displays a black screen in frame (if fullscreen is NO) or opens a movie player view controller and stays forever "Loading...". When I run on device (iOS 7.1), the behavior is the same, but I'm getting this error on the console:

_itemFailedToPlayToEnd: {
    kind = 1;
    new = 2;
    old = 0;
}

When I try to load the exact same video file to stream from web directly, it works perfectly:

//playerController.contentURL = fileURL;
playerController.contentURL = [NSURL URLWithString:@"http://mywebsite.com/Welcome.mp4"];

I've tried different video files but the result is the same. What am I doing wrong?

Upvotes: 3

Views: 2873

Answers (2)

Form
Form

Reputation: 1989

I had the exact same problem, but it was multi-layered in my case.

  • Make sure that you're using -fileURLWithPath: and not -URLWithString: to create your URL, as @tagy22 mentioned. Seems like that does the job for many, however I was already using a file URL, so something was missing.
  • Make sure your local file has an extension (".mp4", for example). MPMoviePlayerController doesn't like files without an extension. A solution if you need to store files without an extension is to create a temporary symlink to the actual file and give MPMoviePlayerController the file URL to that symlink instead of the "real" file.
  • Your symlink should be located somewhere in the iOS Caches directory (~/Library/Caches), else it might not work. I was trying to have MPMoviePlayerController play a symlink that was in the temp directory and it worked in the Simulator but not on my devices.

Creating a symlink to the actual video file

NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
[manager createSymbolicLinkAtURL:linkPath withDestinationURL:actualFilePath error:&error];

Upvotes: 9

tagy22
tagy22

Reputation: 1360

I had a similar problem, streaming files worked fine but could not play local files. The following change solved it for me:

NSURL *fileURL = [NSURL URLWithString:filePath];

to:

NSURL *fileURL = [NSURL fileURLWithPath:filePath];

Upvotes: 8

Related Questions