1

我使用UIImagePickerController創建一個應用程序,它會記錄一個視頻,我保存在文件目錄錄製的視頻,並試圖播放上USE點擊視頻錄製的視頻(要顯示的全部第一幀錄製的視頻以及中間的播放按鈕,就像iPhone的原生應用程序一樣)。播放使用的UIImagePickerController

我能夠錄製視頻並將其保存到文檔目錄中,但無法創建幀和播放。

我試圖移動到新課程,在那裏我可以播放錄製的視頻,但在點擊USE按鈕時碰撞。

這裏是我的代碼

-(void)btnRecord_Press 
{ 
    BOOL canRecordVideo; 

    canRecordVideo = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 

    if (canRecordVideo) 
    { 
     UIImagePickerController *videoRecorder = [[UIImagePickerController alloc]init]; 
     videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera; 
     videoRecorder.delegate=self; 

     videoRecorder.showsCameraControls = TRUE; 

     NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 

     NSArray *videoMediaTypesOnly = [mediaTypes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF CONTAINS %@)",@"movie"]]; 

     BOOL movieOuputPossible = (videoMediaTypesOnly!=nil); 

     if (movieOuputPossible) 
     { 
      videoRecorder.mediaTypes = videoMediaTypesOnly; 

      [self presentViewController:videoRecorder animated:YES completion:nil]; 
     } 

    } 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 

    NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"]; 

    BOOL success = [videoData writeToFile:tempPath atomically:NO]; 


    [self dismissViewControllerAnimated:NO completion:nil]; 

    PlayMovie *play = [[PlayMovie alloc]initWithNibName:@"PlayMovie" bundle:Nil]; 

    [self.navigationController pushViewController:play animated:YES]; 

} 

任何幫助將是明顯的。

感謝

回答

3

我這樣做是使用此代碼。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 

    [self dismissViewControllerAnimated:NO completion:nil]; 

    [self Play]; 

} 

-(void)Play 
{ 

    NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"]; 

    BOOL success = [videoData writeToFile:tempPath atomically:NO]; 

    player = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btnDone_Press) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 

    self._player.shouldAutoplay = NO; 

    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    {   
     screenSize = [[UIScreen mainScreen]bounds].size; 


     if (screenSize.height >480.0f) 
     { 
      player.view.frame = CGRectMake(0, 0, 320, 548); 

     } 
     else 
     { 
      player.view.frame = CGRectMake(0, 0, 320, 460); 

     } 

    } 

    [self.view addSubview:player.view]; 

    self._player.scalingMode = MPMovieScalingModeAspectFit; 

    self._player.fullscreen = YES; 

    [self._player play]; 
}