Zoleas
Zoleas

Reputation: 4879

Problem when trying to play (with MPMoviePlayerViewController) a video/song that I downloaded and stored in a file

Here is my problem.

I have a MPMoviePlayerViewController that play some videos wich are on the web. That part works.

But in order to play them later, without internet connection, I store them on the phone with that piece of code

    NSData * data = [NSData dataWithContentsOfURL:[self dataURL]];

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;

    [data writeToFile:[baseDocumentPath stringByAppendingPathComponent:_itemId] 
       atomically:YES];

That part is ok, I can play the files on my iMac if i take them from the phone.

But after that when i do

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
    videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[baseDocumentPath stringByAppendingPathComponent:file.itemId]]];

There is just a gray Window in the modal viewController. And i get no notifications from the player.

Any ideas?

Thanks.

Upvotes: 2

Views: 1159

Answers (2)

BastiBen
BastiBen

Reputation: 19880

I came across this very same problem today.

It seems that iOS won't load any media files that have the wrong file extension. IMHO this is pretty stupid behavior, as I'm storing my media files with random names (UUIDs).

A quick workaround was to use the following code to create a symlink to the original file and give it the correct extension. Now iOS will happily load the file.

// Create a symlink for iOS as it won't load any files with the wrong extension
NSString *fixedFileName = [fileName stringByAppendingString:@".mp4"];
[[NSFileManager defaultManager] createSymbolicLinkAtPath:fixedFileName 
                                     withDestinationPath:fileName error:NULL];

Hope that helps. We simply ignore the fact that an error occurred, in case the symlink already exists.

Upvotes: 3

Zoleas
Zoleas

Reputation: 4879

Someone found what causes the problem.

The file name has no extension (like .mp4) so the MPMovieController doesn't try to read it (that sounds crazy to me -_- ). If I manually had .mp4 to my video file. the app can read it... I'm gonna append the extension of each file to its name.

Thanks anyway :)

Upvotes: 1

Related Questions