Video recording in iOS programmatically

后端 未结 2 1646
有刺的猬
有刺的猬 2021-02-03 12:44

I am trying to implement functionality like below

Final Recorded Video = \"Capture a video from front camera + Record an audio

2条回答
  •  温柔的废话
    2021-02-03 13:25

    I think this may help you..

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];
    
    AVMutableComposition* mixComposition = [AVMutableComposition composition];
    
    AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
                                        ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
                                         atTime:kCMTimeZero error:nil];
    
    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                                   ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
                                    atTime:kCMTimeZero error:nil];
    
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                                                                          presetName:AVAssetExportPresetPassthrough];   
    
    NSString* videoName = @"export.mov";
    
    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
    NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }
    
    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    DLog(@"file type %@",_assetExport.outputFileType);
    _assetExport.outputURL = exportUrl;
    _assetExport.shouldOptimizeForNetworkUse = YES;
    
    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {      
                // your completion code here
         }       
     }
     ];
    

    Courtesy:-https://stackoverflow.com/a/3456565/1865424

    and you can also check the code for recording video from front camera.

    -(IBAction)cameraLibraryButtonClick:(id)sender{
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {              
            UIImagePickerController *videoRecorder = [[UIImagePickerController alloc]init];  
            videoRecorder.delegate = self;
            NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:videoRecorder.sourceType];
            NSLog(@"Available types for source as camera = %@", sourceTypes);
            if (![sourceTypes containsObject:(NSString*)kUTTypeMovie] ) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                                message:@"Device Not Supported for video Recording."                                                                       delegate:self 
                                                      cancelButtonTitle:@"Yes" 
                                                      otherButtonTitles:@"No",nil];
                [alert show];
                [alert release];
                return;
            }
            videoRecorder.cameraDevice=UIImagePickerControllerCameraDeviceFront;
            videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera;
            videoRecorder.mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];           
            videoRecorder.videoQuality = UIImagePickerControllerQualityTypeLow;
            videoRecorder.videoMaximumDuration = 120;
    
            self.imagePicker = videoRecorder;                 
            [videoRecorder release];
            [self presentModalViewController:self.imagePicker animated:YES];
            newMedia = YES;
        }
        else {
            [self displaysorceError];
        }
    
    
    }
    

    Courtesy:-https://stackoverflow.com/a/14154289/1865424

    If these doesn't work for you..Let me know..But i think this will help you out..

提交回复
热议问题