2012-06-05 37 views
4

我正在尋找一種良好的音頻格式來保存本地語音錄製和通過網絡傳輸。要求是:iOS:用於錄製語音編碼以進行網絡傳輸的音頻格式

  • 體面的質量。這些片段,在接收時將聽取多次
  • 工作流程應該支持修剪和運輸前褪色
  • 體面的文件大小

這是我目前的記錄方法:

// SEE IMA4 vs M4A http://stackoverflow.com/questions/3509921/recorder-works-on-iphone-3gs-but-not-on-iphone-3g 
NSDictionary *recordSettings = 
[[NSDictionary alloc] initWithObjectsAndKeys: 
[NSNumber numberWithFloat: 11025],    AVSampleRateKey, 
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey, 
[NSNumber numberWithInt: 1],      AVNumberOfChannelsKey, 
[NSNumber numberWithBool:NO],     AVLinearPCMIsFloatKey, 
[NSNumber numberWithInt: AVAudioQualityMax],  AVEncoderAudioQualityKey, 
nil]; 

NSError *error = nil; 
self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:self.recordingFile settings:recordSettings error:&error]; 

而且編碼方法:

NSString *file = [NSString stringWithFormat:@"recordingConverted%x.caf", arc4random()]; 
self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent: file]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
if ([fileManager fileExistsAtPath:self.filePath]) { 
    NSError *error; 
    if ([fileManager removeItemAtPath:self.filePath error:&error] == NO) { 
     NSLog(@"removeItemAtPath %@ error:%@", self.filePath, error); 
    } 
} 
NSLog(@"IN: %@", self.recordingFile); 
NSLog(@"OUT: %@", self.filePath); 

AVAsset *avAsset = [AVAsset assetWithURL:self.recordingFile]; 

// get the first audio track 
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio]; 
if ([tracks count] == 0) return nil; 
AVAssetTrack *track = [tracks objectAtIndex:0]; 

// create the export session 
// no need for a retain here, the session will be retained by the 
// completion handler since it is referenced there 
AVAssetExportSession *exportSession = [AVAssetExportSession 
             exportSessionWithAsset:avAsset 
             presetName:AVAssetExportPresetAppleM4A]; 
if (nil == exportSession) return nil; 

// create trim time range 
CMTime startTime = CMTimeMake(self.speakingBeginTime*44100, 44100); 
CMTime stopTime = CMTimeMake((self.speakingBeginTime+[self.duration doubleValue])*44100, 44100); 
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

// create fade in time range 
CMTime startFadeInTime = startTime; 
CMTime endFadeInTime = CMTimeMake((self.speakingBeginTime+RECORDING_INTERVAL)*1.5*44100, 44100); 
CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, 
                 endFadeInTime); 

// setup audio mix 
AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix]; 
AVMutableAudioMixInputParameters *exportAudioMixInputParameters = 
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track]; 

[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 
                timeRange:fadeInTimeRange]; 
exportAudioMix.inputParameters = [NSArray 
            arrayWithObject:exportAudioMixInputParameters]; 

// configure export session output with all our parameters 
exportSession.outputURL = [NSURL fileURLWithPath:self.filePath]; // output path 
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type 
exportSession.timeRange = exportTimeRange; // trim time range 
exportSession.audioMix = exportAudioMix; // fade in audio mix 

// MAKE THE EXPORT SYNCHRONOUS 
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); 
[exportSession exportAsynchronouslyWithCompletionHandler:^{ 
    dispatch_semaphore_signal(semaphore); 
}]; 
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 
dispatch_release(semaphore); 

if (AVAssetExportSessionStatusCompleted == exportSession.status) { 
    return self.filePath; 
    //NSLog(@"AVAssetExportSessionStatusCompleted"); 
} else if (AVAssetExportSessionStatusFailed == exportSession.status) { 
    // a failure may happen because of an event out of your control 
    // for example, an interruption like a phone call comming in 
    // make sure and handle this case appropriately 
    NSLog(@"AVAssetExportSessionStatusFailed %@", exportSession.error.localizedDescription); 
} else { 
    NSLog(@"Export Session Status: %d", exportSession.status); 
} 

目前,performan在一個3秒的音頻片段上是:62,228字節的PCM和36,654字節的編碼。似乎我可以做得更好。

回答