简体   繁体   English

如何从目录读取文件

[英]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 该项目将视频文件保存在以下路径中: 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? 我想使用MPMoviePlayerController播放文件,如何访问该文件?

Try this :- 尝试这个 :-

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

Here in fullPath you will have path you mentioned above. fullPath您将具有上面提到的路径。

For more detail on how to play video, refer this tutorial 有关如何播放视频的更多详细信息,请参阅本教程

Hope this helps you.. 希望这对您有帮助。

Try this code mate, 试试这个代码伴侣,

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];

Firslty load the path and file URL: 首先加载路径和文件URL:

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

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

Then load up MPMoviePlayerController with the URL 然后使用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 
{        

}

If you store the above URL somewhere 如果您将上述网址存储在某处

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];

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM