Bryan1234321
Bryan1234321

Reputation: 63

Record and play audio same time

I am trying to make the audio record and play back at the same time. Here is the code. It will only record and play back later. I want it to play back simultaneously over the speakers. Any help. I am not even sure I am using the right classes.

 AVAudioSession * audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];

    temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:@"VoiceFile"]]];

    recorder = [[AVAudioRecorder alloc] initWithURL:temporaryRecFile settings:nil error:nil];

    [recorder setDelegate:self];
    [recorder prepareToRecord];
    [recorder record];

    AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
    player.volume = 1;
    [player play];

Upvotes: 0

Views: 1245

Answers (2)

bateristt
bateristt

Reputation: 176

You are using correct classes but I think you must add AutioUnits also.

Apple has a sample code aurioTouch2 and I think it contains what you need.

Upvotes: 1

Coeffect
Coeffect

Reputation: 8866

Are you recording from the iPhone microphone? Chances are you'll get feedback if you try to play the recording back while recording it. The microphone will hear what the speakers are playing and rerecord it at a slight delay. This would snowball until your recording sounds like a train wreck.

Upvotes: 1

Related Questions