Jose Luis
Jose Luis

Reputation: 3361

MPMoviePlayerViewController not working

I have been roaming the internet for a solution I really don't know what am I doing wrong. I have been with this problem a few days now. I save the video at the following path that should be accessible to the application (Right?)

//NSDocumentDirectory doesn't work either.
NSArray *newPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory,
                                                       NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:@"%@/WebSurg", 
                                                     [newPath objectAtIndex:0]];
// Check if the directory already exists
if (![[NSFileManager defaultManager] fileExistsAtPath:moviesDirectory]) {
    // Directory does not exist so create it
    [[NSFileManager defaultManager] createDirectoryAtPath:moviesDirectory 
                      withIntermediateDirectories:YES attributes:nil error:nil];
}

I show the contents of this directory in a tableView in the application. When a row is tapped it should play the video. But it doesn't. It shows me the MPMoviePlayerViewController modal view and then hides it after probably what is 1 second. This is the code I use to play it: I tried two ways of getting the path to no avail.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *moviesPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, 
                                                              NSUserDomainMask, YES);
    NSString *moviesDirectory = [NSString stringWithFormat:@"%@/WebSurg", 
                                                       [moviesPath objectAtIndex:0]];
    NSString *movie = [self.tableData objectAtIndex:indexPath.row];
    NSString *moviePath = [NSString stringWithFormat:@"%@/%@", 
                                                         moviesDirectory, movie];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; 
    NSLog(@"MOVIEPATH: %@", moviePath);

    NSString *alternatePath = [NSString stringWithFormat:@"%@/%@", 
                                            [[NSBundle mainBundle] resourcePath], movie];
    NSURL *alternateMoviePath = [NSURL fileURLWithPath:moviePath];
    movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:alternateMoviePath];
    movieViewController.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
    NSLog(@"Movie Load State: %d", [[movieViewController moviePlayer] loadState]);
    NSLog(@"Alternate movie Path: %@", alternatePath);
    [self presentMoviePlayerViewControllerAnimated:movieViewController];
    [movieViewController.moviePlayer play];
    [self checkAndPlay];
}

- (void) checkAndPlay {
    if ([[self.movieViewController moviePlayer] loadState] == MPMovieLoadStateUnknown) { 
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:
                               @selector(checkAndPlay) userInfo:nil repeats:NO];
    } else {
        [self.movieViewController setModalTransitionStyle:
                                   UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:movieViewController animated:YES];
    }
}

And these are the results of the console:

2012-10-08 10:14:52.392 WebsurgTemplates[3722:17903] MOVIEPATH: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Movies/WebSurg/FirstVideo.mp4
2012-10-08 10:14:52.459 WebsurgTemplates[3722:17903] Movie Load State: 0
2012-10-08 10:14:52.460 WebsurgTemplates[3722:17903] Alternate movie Path: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/WebsurgTemplates.app/FirstVideo.mp4

I would greatly appreciate any suggestions and help!!

UPDATE

I made no progress so far. I managed to log some other data to the console, some info that may help more at solving this problem. I tried to make a blank project taking the direct download of the video as link to play the video but it didn't work. What happens is exactly the same thing. jaydee3 said that maybe it was due because I had probably no access to NSMoviesDirectory. So I changed to NSDocumentDirectory but that didn't solve the problem. I checked that the file exists and the format in which it is saved so it can be readable by the player. Still it doesn't work. I don't know what am I doing wrong. Thanks again for the suggestions/help.

Here the results of the debug. more complete:

if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath]) {
    NSLog(@"FILE EXISTS");
    CFStringRef fileExtension = (__bridge CFStringRef) [moviePath pathExtension];
    CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

    if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
    else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
    else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");
}

RESULTS
[6343:17903] MOVIEPATH: /Users/myname/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Documents/FirstVideo.mp4
[6343:17903] FILE EXISTS
[6343:17903] It's a movie

Upvotes: 1

Views: 438

Answers (1)

Jose Luis
Jose Luis

Reputation: 3361

Okay so I managed to solve the problem and find the culprit.

In a brief note it was because the downloaded movie wasn't being saved properly (I am currently investigating the possible reasons why). And because of this the player was trying to play a file that existed, was in the correct format and the correct name but that was empty. I found this out by logging all the file sizes after download and transfer and play.

Now being more descriptive the issue was that I was downloading the movie to the NSCachesDirectory and then saving it to the NSDocumentDirectory. I found this because I started to wonder if it really found the file and if the file was "edible". It now plays the movie fine as I download it directly to the NSDocumentDirectory. Now I have to solve just in case the connection goes down. As saving in the NSCachesDirectory solved that automatically. I am open to suggestions on that. here is the code that didn't work to transfer the data from the NSCachesDirectory to NSDocumentDirectory:

NSArray *paths = NSSearchPathForDirectoriesInDomains
                // HERE I changed NSCachesDirectory to NSDocumentDirectory fixed it
                            (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:
                                                  @"DownloadedVideo.mp4"];
NSArray *newPath = NSSearchPathForDirectoriesInDomains
                                  (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:@"%@", 
                                                [newPath objectAtIndex:0]];
self.downloadOperation = [ApplicationDelegate.mainDownloader downloadVideoFrom:
                                    @"http://www.blablabla.web/iphone/download.php"                                    
                                            toFile:downloadPath]; 

Upvotes: 1

Related Questions