2011-12-02 86 views
0

在我的iPhone應用程序我正在使用ImagePickerview視頻並將其存儲到文檔目錄iPhone:無法播放視頻在iOS 5中

爲iOS4的(設備的iPod)我可以播放存儲在文件目錄,但對iOS 5的視頻(設備iPad)相同的代碼不適用於播放視頻。

所以對於iOS 5中有用於存儲和文檔目錄播放視頻有什麼不同的方式

(我也有圖書館目錄試過)

-(IBAction)saveVideo:(id)sender 
    { 
    imagepicker = [[UIImagePickerController alloc] init]; 
     imagepicker.delegate=self; 
    if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     [imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
     [imagepicker setMediaTypes:[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]]; 

     [imagepicker setCameraDevice:UIImagePickerControllerCameraCaptureModeVideo]; 

     [self presentModalViewController:imagepicker animated:YES]; 

    } 


} 



    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 


    NSMutableString *tempPath = [[[info valueForKey:@"UIImagePickerControllerMediaURL"] absoluteString] mutableCopy]; 
    NSString *newPath1 = [NSString stringWithFormat:@"myVideo.mov"]; 
    [tempPath replaceOccurrencesOfString:@"file://localhost/private/" withString:@"/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempPath length])]; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSError *error = nil; 
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], newPath1]; 
    NSString *newPath = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], newPath1]; 


    BOOL success = [manager copyItemAtPath:tempPath toPath:newPath error:&error]; 

    if(error){   
    NSLog(@"New Path: %@", newPath); 
    NSLog(@"Error: %@", error); 
    } 

    if(success) 
    { 
    NSLog(@"Succeed");  
    } 
    [self dismissModalViewControllerAnimated:YES]; 
    } 


    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    { 
    [picker dismissModalViewControllerAnimated:YES]; 
    } 


    -(IBAction)playVideo:(id)sender 
    { 
    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [path objectAtIndex:0]; 
    NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"myVideo.mov"]; 

    NSURL *url = [[NSURL alloc] initWithString:myDBnew]; 
    NSLog(@"URL== %@",url); 
    moviePlayer = [[MPMoviePlayerController alloc] 
        initWithContentURL:url]; 

    //  [[NSNotificationCenter defaultCenter] addObserver:self 
    //             selector:@selector(moviePlayBackDidFinish:) 
    //              name:MPMoviePlayerPlaybackDidFinishNotification 
    //             object:moviePlayer]; 

    moviePlayer.controlStyle = MPMovieControlStyleDefault; 
    moviePlayer.shouldAutoplay = YES; 
    [self.view addSubview:moviePlayer.view]; 
    [moviePlayer setFullscreen:YES animated:YES]; 
    } 

出了什麼問題?

這裏是代碼

如果我添加視頻文件到我的項目,並使用下面的代碼在播放視頻的唯一的事情是它不會從文件目錄播放視頻

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"myVideo" ofType:@"mov"]]; 
    moviePlayer = [[MPMoviePlayerController alloc] 
        initWithContentURL:url]; 

即使在通過flittering iTunes我可以看到文件:「myVideo.mov」,在設備中運行應用程序後意味着代碼錄製和存儲視頻,但無法讀取或播放。

請幫助和建議。

感謝

+0

我的建議的方式進行網址

NSURL *url = [[NSURL alloc] initWithString:myDBnew]; 

是向我們展示您用來存儲和播放視頻的代碼,否則我們不知道您做錯了什麼。 – sosborn

+0

我已經發布完整的代碼請幫助我 – ios

回答

0

在這裏,在我的代碼的問題是我創建不爲iOS5的工作,所以我的代碼替換

NSURL *url = [[NSURL alloc] initFileURLWithPath:<#(NSString *)#> isDirectory:<#(BOOL)#>];