iphone - 将音频录制到文件并从中播放

标签 iphone ios objective-c

我有一个广播应用程序,我可以播放广播并录制它以寻求求婚。

我有这个ExtAudioFileRef作为记录文件:

ExtAudioFileRef mRecordFile;

这就是我创建它的方式:

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

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

status = ExtAudioFileWrite(mRecordFile, frames, data);

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

最佳答案

我使用 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;
}

关于iphone - 将音频录制到文件并从中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17143297/

相关文章:

ios - swift 数组 : fatal error index out of range

ios - 如何在其他开发人员设备上安装我的 xcode 项目进行测试

ios - Swift 3 迁移清除 NSUserDefault 数据

objective-c - Swift 的 CGRect 和 CGSize 扩展在 Objective-C 中不可见

ios - NavigationBar 标题没有出现

ios - 如何设置 UIScrollview 和动画的优先级

iphone - Apple 在应用审核期间会在哪些设备上测试您的应用?

objective-c - 未调用 mapViewWillStartLoadingMap

ios - 将 NSData 转换为 NSString

iphone - 在 UIImagePicker 中设置焦点