簡體   English   中英

如何從目錄讀取文件

[英]how to read files from directory

我有一個別人寫的項目。 該項目將視頻文件保存在以下路徑中: file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4

我想使用MPMoviePlayerController播放文件,如何訪問該文件?

嘗試這個 :-

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

fullPath您將具有上面提到的路徑。

有關如何播放視頻的更多詳細信息,請參閱本教程

希望這對您有幫助。

試試這個代碼伴侶,

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

首先加載路徑和文件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 
{        

}

如果您將上述網址存儲在某處

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

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM