2013-03-11 5 views
1

AVMutableComposition을 사용하여 오디오 (녹음 된)와 비디오 (.mp4)를 섞으려고 시도했습니다. 즉, 믹싱 후 2 개의 파일을 병렬로 재생해야합니다. 여기에 코드 i가 있습니다. 사용 : temporaryRecFile 촬영 한 파일의 URL 경로 audiosURL 비디오 파일의 URL 경로 결과AVMutableComposition 비디오 사운드 트랙 제거

NSURL *audiosURL =[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"dam3.mp4"] ofType:nil]]; 

     NSLog(@"SOMEDATA IS THERE "); 
     AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:temporaryRecFile options:nil]; 
     AVURLAsset* audio2Asset = [[AVURLAsset alloc]initWithURL:audiosURL options:nil]; 

     AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

    AVMutableCompositionTrack *compositionCommentary2Track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionCommentary2Track insertTimeRange:CMTimeRangeMake(kCMTimeZero, audio2Asset.duration) ofTrack:[[audio2Asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

     NSLog(@"audio =%@",audioAsset); 
     AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
     [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] 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 = AVFileTypeQuickTimeMovie; 
    //@"com.apple.quicktime-movie"; 
     NSLog(@"file type %@",_assetExport.outputFileType); 
     _assetExport.outputURL = exportUrl; 
     _assetExport.shouldOptimizeForNetworkUse = YES; 



     [_assetExport exportAsynchronouslyWithCompletionHandler: 
     ^(void) 
     { 
      switch (_assetExport.status) 
      { 
       case AVAssetExportSessionStatusCompleted: 
        // export complete 

        NSLog(@"Export Complete"); 
        // From Here I want play movie using MPMoviePlayerController.<<<--------- 
        NSString *fileNamePath = @"sound_record.mov"; 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; 
        NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath]; 


        if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) { 

         NSFileManager *fileManager = [NSFileManager defaultManager]; 
         [fileManager removeItemAtPath: oldappSettingsPath error:NULL]; 

        } 
        NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath]; 
        [[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil]; 
        [audioAsset release]; 
        [audio2Asset release]; 
        [_assetExport release]; 
        [self performSelectorOnMainThread:@selector(playVideo:) withObject:documentDirectoryURL waitUntilDone:NO]; 

        break; 
       case AVAssetExportSessionStatusFailed: 
        NSLog(@"Export Failed"); 
        NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 

        //    export error (see exportSession.error) 
        break; 
       case AVAssetExportSessionStatusCancelled: 
        NSLog(@"Export Failed"); 
        NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 

        //    export cancelled 
        break; 

      } 
     } 
     ]; 

에게있다, 보낸 비디오 성공적으로 비디오와 함께 녹음 된 오디오를 재생하지만, 영상의 사운드 트랙이었다 삭제되었으므로 내 보낸 .mov는 소리가없는 비디오 이미지만으로 녹음 된 오디오 파일을 재생합니다. 왜 그런 일이 발생합니까?

답변

1

별도의 트랙으로 비디오의 오디오를 추가해야합니다. 따라서 한 개의 비디오 트랙과 두 개의 오디오 트랙을 갖게됩니다. 코드에서 비디오의 오디오를 추가하지 않아도됩니다.