簡體   English   中英

如何播放文檔目錄中的視頻(按名稱)?

[英]How can i play video (By Name) from document directory ?

在我的應用程序中,我將視頻存儲在文檔目錄中,名稱為'video2.MOV' ,我想播放它。

這里的問題是我無法從文檔目錄中獲取此視頻(名稱為'video2.MOV'

我的代碼:

-(void)playVideo:(NSTimer *)theTimer
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"video2.MOV"];
    NSLog(@"filePath - %@",filePath);

    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"contentPath - %@",content);

    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:content]];
    [[CCDirector sharedDirector] presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
}

在控制台中,每次獲得contentPath均為NULL ,因此可能無法播放視頻。

在這里,我的錯是什么。 請給我關於這個問題的建議。

嘗試這個:

- (NSString*)GetDocumentFilePath:(NSString*)filename
{
    NSLog(@"%@",filename);
    // NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString* file = [[NSString alloc]initWithFormat:@"%@",filename];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:file];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]) //4
    {
        //NSString *bundle = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"]; //5

        //[fileManager copyItemAtPath:bundle toPath:path error:&error]; //6
    }  

    return path;
}

並獲得如下文件路徑:

 NSURL *URL = [NSURL fileURLWithPath:[self GetDoucmentPath:path]];


MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:pptURL];
    theMovie.scalingMode=MPMovieScalingModeAspectFill;

    theMovie.view.frame = CGRectMake(60, 20, 700, 500);
    theMovie.view.center = self.view.center;

    //[self.view addSubview:theMovie.view];

    // Register for the playback finished notification. 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(onStop) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:theMovie];

    // Movie playback is asynchronous, so this method returns immediately. 

    [self.view addSubview:theMovie.view];
    [theMovie play];

希望能幫助到你!!

與其使用content filePath使用filePath來訪問電影文件:

 MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

暫無
暫無

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

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