0
從文件目錄播放電影

我有這個功能寫NSData的記錄字典 -無法在iOS版

- (void)writeData 
{ 
    NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v"; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:str]; 
    NSLog(@"%@",path); 
    NSLog(@"About to Write Data"); 
    NSData* data0 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]]; 
    [data0 writeToFile:path atomically:YES]; 
    NSLog(@"Writting Data Completed"); 
} 

後來我讀了這些數據,並嘗試播放使用的MPMoviePlayerController視頻,但不幸的是這 不PLAY-

- (void)readData 
{ 
    NSString *str = @"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:str]; 
    NSURL* url = [[NSURL alloc] initFileURLWithPath:path];  
    NSLog(@"url:%@",url); 

    NSError *error = nil; 
    BOOL isReacahble = [url checkResourceIsReachableAndReturnError:&error]; 
    NSLog(@"isReachable :%i,%@",isReacahble,error); 

    BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:path]; 

    if(fileExist) 
    { 
     NSLog(@"File Exist in path"); 
    } 
    else 
    { 
     NSLog(@"NO File Exist"); 
    } 

    MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url]; 
    [moviePlayer prepareToPlay]; 
    NSLog(@"movie player:%@",moviePlayer); 
    [moviePlayer setControlStyle: MPMovieControlStyleFullscreen]; 
    [moviePlayer.view setFrame:CGRectMake(0, 0,320, 480)]; 
    [self.view addSubview:moviePlayer.view]; 
    moviePlayer.shouldAutoplay = YES; 
    [moviePlayer play]; 
} 

當我嘗試玩這個它顯示了在無差錯

2013-01-17 11:21:07.067 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay for pause 
2013-01-17 11:21:07.068 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay 
2013-01-17 11:21:07.090 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2013-01-17 11:21:07.091 VideoCheck[338:907] movie player:<MPMoviePlayerController: 0x21078250> 
2013-01-17 11:21:07.093 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2013-01-17 11:21:07.102 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay 
2013-01-17 11:21:07.113 VideoCheck[338:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 
2013-01-17 11:21:07.114 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 
2013-01-17 11:21:07.115 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay 
2013-01-17 11:21:07.121 VideoCheck[338:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0 
2013-01-17 11:21:07.149 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay 

有人可以運行代碼,並提供一些有關這裏發生的事情的信息??請有人幫助我。我正在使用iOS 6.

+0

我發現這個問題,這是完全愚蠢的事情,如果我們使用大量的'/'大名稱,UIKit無法正確解析數據,還有1個事情要用MPMoviePlayer播放視頻,我們必須使用一些視頻格式如URL路徑中的.m4v擴展名,否則MPMoviePlayer將無法播放它。 – russell

回答

0

在您的wrieData方法中,不是將URL作爲文件的名稱,而只給出video的名稱。像

NSString *path = [documentsDirectory stringByAppendingPathComponent:[str lastPathComponent]]; 

並做readData方法一樣了。