我已使用 AVAudio Recorder 进行录制。我正在发布此代码
-(void)startRecording
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
NSError *error;
NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSString *tempDir = NSTemporaryDirectory();
NSString *filename=[NSString stringWithFormat:@"sound%d.caf",count];
NSString *soundFilePathString = [tempDir stringByAppendingString:filename];
NSURL *urlSoundFile=[[NSURL alloc]initFileURLWithPath:soundFilePathString];
NSLog(@"%@",soundFilePathString);
soundRecorder = [[AVAudioRecorder alloc] initWithURL:urlSoundFile settings:recordSettings error: &error];
[soundRecorder prepareToRecord];
[soundRecorder setMeteringEnabled:YES];
[soundRecorder updateMeters];
[soundRecorder record];
double power = [soundRecorder peakPowerForChannel:0];
NSLog(@"%f",power);
if (power > .01f && soundRecorder.recording==NO)
{
[soundRecorder record];
} else if (power < .01f && soundRecorder.recording==YES)
{
[soundRecorder stop];
}
if(!soundRecorder.recording)
{
[soundRecorder updateMeters];
double peakPowerForChannel = pow(10, (ALPHA* [soundRecorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
NSLog(@"%f",[soundRecorder peakPowerForChannel:0]);
NSLog(@"%f",peakPowerForChannel);
if (peakPowerForChannel>.01 && flagForContinuousRecording)
{
flagForContinuousRecording=NO;
// [timerForUserVoiceRecognition invalidate];
soundRecorder.delegate = self;
[soundRecorder record];
}
if (peakPowerForChannel<.01 && !flagForContinuousRecording)
{
[soundRecorder stop];
[self playRecodedAudio];
}
}
}
录制后,我正在使用以下编写的功能播放录制的音频
-(void)playRecodedAudio
{
NSString *tempDir = NSTemporaryDirectory();
NSString *filename=[NSString stringWithFormat:@"sound%d.caf",count];
// NSString *filename=[NSString stringWithFormat:@"mario-theme.mp3"];
NSString *soundFilePathString = [tempDir stringByAppendingString:filename];
//[self modifySpeedOf:(CFURLRef)[NSURL URLWithString:soundFilePathString] byFactor:1 andWriteTo:(CFURLRef)[NSURL URLWithString:soundFilePathString]];
[[CDAudioManager sharedManager].soundEngine unloadBuffer:soundSlot];
[[CDAudioManager sharedManager].soundEngine loadBuffer:soundSlot filePath:soundFilePathString];
[[[CDAudioManager sharedManager] soundEngine] setMasterGain:2.0];
//[[CDAudioManager sharedManager].soundEngine playSound:soundSlot channelGroupId:0 pitch:1.5f pan:0.0f gain:5.0f loop:NO];
[[CDAudioManager sharedManager].soundEngine playSound:soundSlot sourceGroupId:0 pitch:1.5f pan:0.0f gain:5.0f loop:NO];
flagForContinuousRecording=YES;
}