2013-02-19 213 views
0

我收到了一個其他人寫的項目。 該項目保存視頻文件在這條路徑,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4 如何從目錄中讀取文件

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

回答

1

Firslty加載路徑和文件網址:

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

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

然後加載MPMoviePlayerController與U RL

- (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 
{   

} 
+0

感謝您的嘗試,但電影不播放 – 2013-02-19 09:33:41

+0

編輯:我也只是測試了上述,它工作正常。 – Dev2rights 2013-02-19 09:40:39

+0

不工作我越來越我得到一個錯誤 errorLog MPMovieFinishReasonPlaybackError – 2013-02-19 12:27:06

1

試試這個: -

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

在這裏,在fullPath,你將有你上面提到的路徑。

有關如何播放視頻更詳細,請參閱this tutorial

希望這有助於你..

+0

? – 2013-02-19 09:31:49

+0

@MidhunMP其實我有用於播放許多視頻的整數變量,無論如何編輯答案。謝謝:) – 2013-02-19 09:52:54

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

希望你爲什麼要使用`stringWithFormat`,幫助