2016-11-08 65 views
0

我想在iOS的(AVAudioRecorder)錄製下面的代碼的音頻工作記錄罰款AVAudioRecorder 256 Kbps的iOS中

_fileName = [NSString stringWithFormat:@"Record_%@.m4a",[DateAndTimeUtil stringFromDate:[NSDate date] withFormatterString:@"HH_mm_ss_dd_MM_yyyy"]];NSArray *pathComponents = [NSArray arrayWithObjects: 
           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], 
           _fileName, 
           nil]; 

NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; 

// Setup audio session 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

// Define the recorder setting 
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 

// Initiate and prepare the recorder 
audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil]; 
audioRecorder.delegate = self; 
audioRecorder.meteringEnabled = YES; 
[audioRecorder prepareToRecord];` 

的問題是,錄製的文件顯示比特率設置爲44 Kbps的,但我想記錄的音頻平均比特率爲256Kbps,首選AAC編解碼器,但也兼容MP3編解碼器和MP4音頻編解碼器。

請幫我一把。

回答

0
_fileName = [NSString stringWithFormat:@"Record_%@.mp4",[DateAndTimeUtil stringFromDate:[NSDate date] withFormatterString:@"HH_mm_ss_dd_MM_yyyy"]]; 
NSArray *pathComponents = [NSArray arrayWithObjects: 
          [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], 
          _fileName, 
          nil]; 

NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; 

// Setup audio session 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

// Define the recorder setting 
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 
[recordSetting setValue:[NSNumber numberWithInteger:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; 
[recordSetting setValue:[NSNumber numberWithInt:32] forKey:AVLinearPCMBitDepthKey]; 
[recordSetting setValue:[NSNumber numberWithInt:128000] forKey:AVEncoderBitRatePerChannelKey]; 
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVEncoderBitDepthHintKey]; 


// Initiate and prepare the recorder 
audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil]; 
audioRecorder.delegate = self; 
audioRecorder.meteringEnabled = YES; 
[audioRecorder prepareToRecord]; 

通過使用上面的代碼,我達到了一個積極的解決方案,因爲它能夠記錄音頻與比特率信息。音頻具有幾乎256Kbps的比特率。