如何从目录中读取文件

时间:2013-02-19 08:47:56

标签: iphone ios nsdocumentdirectory

我有一个别人写的项目。 项目将视频文件保存在此路径file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4

我想用MPMoviePlayerController播放文件,我该如何访问该文件?

4 个答案:

答案 0 :(得分:1)

试试这个: -

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

fullPath中,您将获得上面提到的路径。

有关如何播放视频的详细信息,请参阅this tutorial

希望这可以帮助你..

答案 1 :(得分:1)

试试这个代码配偶,

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSString *videoFilePath = [documentDirectory stringByAppendingPathComponent:@"output.mp4"];
    NSURL *videoURL = [NSURL URLWithString:videoFilePath];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

答案 2 :(得分:1)

Firslty加载路径和文件URL:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];

NSString *localFilePath = [photoDirectoryPath stringByAppendingPathComponent:@"output.mp4"];

然后使用URL

加载MPMoviePlayerController
- (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 
{        

}

答案 3 :(得分:1)

如果您将上述网址存储在某处

NSString *fullURL = @"/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4"
NSURL *movieURL = [NSURL fileURLWithPath: isDirectory:YES]; //If YES not works use NO
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

希望有所帮助