Search code examples
iphoneiosnsdocumentdirectory

how to read files from directory


i got a project that someone else write. the project saves video file in this path,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4

i want to play the file with MPMoviePlayerController, how can i access the file?


Solution

  • Firslty load the path and file URL:

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *localFilePath = [photoDirectoryPath stringByAppendingPathComponent:@"output.mp4"];
    

    Then load up MPMoviePlayerController with the URL

    - (void)initMoviePlayer 
    {
    didExit = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePreloadDidFinish:) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:mMoviePlayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil];
    
    NSLog(@"initMoviePlayer: %@ ",[self getMovieURL]);
    
    mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
    
    mMoviePlayer.shouldAutoplay = YES;
    
    mMoviePlayer.view.hidden = YES;
    
    [mMoviePlayer setControlStyle:MPMovieControlStyleNone];
    
    mMoviePlayer.view.frame = [self.view bounds];
    
    mMoviePlayer.scalingMode = MPMovieScalingModeNone;
    
    [[self view] addSubview:[mMoviePlayer view]];
    
    [mMoviePlayer play];
    
    }
    
    - (void) moviePreloadDidFinish:(NSNotification*)notification {
    
    // start playing the movie
    
        mMoviePlayer.view.hidden = NO;
    
        animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                    target:self 
                                                  selector:@selector(playMovie:) 
                                                  userInfo:nil 
                                                   repeats:NO];
    }
    
    - (void) moviePlayBackDidFinish:(NSNotification*)notification 
    {        
    
    }