簡體   English   中英

播放保存在應用程序文檔目錄中的視頻文件

[英]Playing a video file saved in the app's documents directory

我有一個視頻文件保存在我的應用程序的文檔文件夾中的本地目錄中。 我想在用戶點擊我創建的嵌入式表格視圖中的項目時播放該文件。 我播放視頻的代碼如下:

NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* path = [documentPath stringByAppendingPathComponent:@"DemoRecording.mp4"];
NSURL* movieURL = [NSURL fileURLWithPath: path];
[player.view setFrame: self.view.bounds];
[self.view addSubView: player.view];
[player play];
MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
[self presentMoviePlayerViewControllerAnimated: moviePlayer];

視頻無法播放。 路徑正確,文件存在。
我知道這是一個重復的問題。 我已經提到了這些鏈接:
使用Cocoa-Touch從文檔目錄播放下載的視頻
MPMoviePlayer加載並播放保存在應用文檔中的電影
但無法找到明確的答案。
請幫我解決這個問題。 我正在使用iOS 5.1.1,如果這改變了什么。

編輯:

它確實有效。 我忘了將URL傳遞給電影播放器​​。

你可以像這樣從文檔目錄播放視頻: -

-(IBAction)playVideo
{
    NSURL *vedioURL;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSLog(@"files array %@", filePathsArray);        

    NSString *fullpath;

    for ( NSString *apath in filePathsArray )
    {
        fullpath = [documentsDirectory stringByAppendingPathComponent:apath];       
        vedioURL =[NSURL fileURLWithPath:fullpath];
    }
    NSLog(@"vurl %@",vedioURL);
    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];     
}

注意

請注意,請勿忘記包含所請求的Framework或連接Delegate

你錯過了/在DemoRecording.mp4之前

它應該是

NSString* path = [documentPath stringByAppendingPathComponent:@"/DemoRecording.mp4"];

你也在宣布player聲明之前編寫player聲明。

暫無
暫無

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

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