我有一个收音机应用程序,我播放收音机,并记录它寻求建议。
我有ExtAudioFileRef
个记录文件:
ExtAudioFileRef mRecordFile;
这就是我创造它的方式:
ExtAudioFileCreateWithURL(destinationURL, kAudioFileCAFType, &mRecordFormat, NULL, kAudioFileFlags_EraseFile,
&mRecordFile);
这就是我写数据的方式:
status = ExtAudioFileWrite(mRecordFile, frames, data);
我希望能够播放此引用的音频,同时能够将其他音频缓冲区写入文件。这是可能的吗?
答案 0 :(得分:1)
我已经使用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;
}