2011-03-31 63 views

回答

0

HI Monish, 音频我可以尝试在AVAudioRecorded.I附加代码如下。

AVMutableComposition* composition = [AVMutableComposition composition]; 
     AVURLAsset* audioAsset1 = [[AVURLAsset alloc]initWithURL:recorderfileO options:nil]; 
     AVURLAsset* audioAsset2 = [[AVURLAsset alloc]initWithURL:recorderfile1 options:nil]; 

     AVMutableCompositionTrack *audioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio 
                      preferredTrackID:kCMPersistentTrackID_Invalid]; 

     [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration) 
          ofTrack:[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
           atTime:kCMTimeZero 
           error:&error]; 
     [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset2.duration) 
          ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
           atTime:audioAsset1.duration 
           error:&error]; 
     //NSLog(@"tt :%@",audioTrack1); 

     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition 
                       presetName:AVAssetExportPresetAppleM4A]; 
     NSLog (@"can export: %@", exportSession.supportedFileTypes); 
     // NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     //  NSLog(@"Dirs : %@",dirs); 
     //  NSString *documentsDirectoryPath = [dirs objectAtIndex:0]; 

     NSString *documentsDirectoryPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

     NSDate *today = [NSDate date]; 
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
     [dateFormatter setDateFormat:@"HH:mm:ss"]; 
     NSString *currentTime = [dateFormatter stringFromDate:today]; 
     [dateFormatter release]; 
     NSLog(@"Using File called: %@",currentTime); 
     NSString * exportPath1 = [NSString stringWithFormat:@"%@.m4a",currentTime]; 
     exportPath = [documentsDirectoryPath stringByAppendingPathComponent:exportPath1]; 
     [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
     NSLog(@"Doc Path: %@", exportPath); 
     exportURL = [NSURL fileURLWithPath:exportPath]; 
     exportSession.outputURL = exportURL; 
     NSLog(@"Doc URL: %@",exportURL); 
     exportSession.outputFileType = AVFileTypeAppleM4A; 
     NSLog(@"File Types: %@" , exportSession.supportedFileTypes); 
     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      NSLog (@"status is %d", 
        exportSession.status); 
      Play.enabled = NO; 
      if (exportSession.status==3) { 
       Play.enabled = YES; 
      } 
      switch (exportSession.status) 
      { 
       case AVAssetExportSessionStatusFailed: 

       case AVAssetExportSessionStatusCompleted: 
       { 

        NSFileManager * fm1 = [NSFileManager defaultManager]; 
        [fm1 removeItemAtPath:[recorderfile1 path] error:&error]; 

        NSFileManager * fm2 = [NSFileManager defaultManager]; 
        [fm2 removeItemAtPath:[recorderfileO path] error:&error]; 
        recorderfileO = exportURL; 

        break; 
       } 
      }; 
      [audioAsset1 release]; 
      [audioAsset2 release]; 
      [audioTrack1 release]; 
     }]; 

使用此代码恢复录制音频。