0

我有一个收音机应用程序,我播放收音机并录制它以寻求建议。

我有这个ExtAudioFileRef记录文件:

ExtAudioFileRef mRecordFile;

这就是我创建它的方式:

ExtAudioFileCreateWithURL(destinationURL, kAudioFileCAFType, &mRecordFormat, NULL, kAudioFileFlags_EraseFile,
                          &mRecordFile);

这就是我向它写入数据的方式:

status = ExtAudioFileWrite(mRecordFile, frames, data);

而且我希望能够播放此参考中的音频,同时能够将其他音频缓冲区写入文件。有可能吗?

4

1 回答 1

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;
}
于 2013-06-17T09:07:24.250 回答