1

I am continuing this question from my old post. I read much content for audio recording and playing using Audio queues, audio bufferes, Unfortunately due to less time I compromised and planning to use AVFondation frame work for recording and playing of recorder audio.

My idea: creating one instance of AVRecorder and record through micro phone, same audio planning to play through Iphone speaker using AVAudioPlayer (I ll use multiple instances)

Please bare my tons of lines for recording and playing

-(id)startAudioRecorder:(NSUInteger)viewTag {
    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    //Set the general audio session category
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];
    //Make the default sound route for the session be to use the speaker
    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
    //Activate the customized audio session
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
    self.audioRecorder=nil;
    //AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    //[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init] ;
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//kAudioFormatAppleIMA4,kAudioFormatAAC
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    NSString *absolutePath=[self getAbsoluteAudioFilePath:viewTag];
    NSError *audioRecorderError=nil;
    NSURL *absoluteUrl=[NSURL fileURLWithPath:absolutePath];
    self.audioRecorder=[[[AVAudioRecorder alloc] initWithURL:absoluteUrl settings:recordSetting error:&audioRecorderError] autorelease];
    NSLog(@"%s audioRecorder created=%@",__func__,self.audioRecorder);
    BOOL isDurationAccepted=[self.audioRecorder recordForDuration:1800.0];
    BOOL record=[self.audioRecorder record];
    [recordSetting release];
    recordSetting=nil;
    //if(error !=NULL) *error=audioRecorderError;
    return nil;
}






   -(id)playRecordedAudio:(NSUInteger)viewTag
 {

            NSURL *absoluteUrl=[NSURL fileURLWithPath:[[self getAbsoluteAudioFilePath:viewTag]  stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];
            NSError *audioPlayerError=nil;

            recordingView.audioPlayer=[[[AVAudioPlayer alloc] initWithContentsOfURL: absoluteUrl error:&audioPlayerError] autorelease] ;
            recordingView.audioPlayer.delegate=self;
            [recordingView.audioPlayer prepareToPlay];
            recordingView.audioPlayer.numberOfLoops=-1;
            UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
            [recordingView.audioPlayer play];
            return nil;
        }

The above code perfectly working as I expected. But when I am looking for recording->playing, again recording(before finishing currently playing audio) this time currently playing audio coming to microphone(ear phones) instead of playing through speakers.

Now the problem is, I am unable to record and play simultaneously. I am expecting record(even audio playing) always through earphones and multiple audios play through phone speaker.

Please suggest me changes in my above code to come out of this situation. All your comments helpful.

Community
  • 1
  • 1
ajay
  • 3,245
  • 4
  • 31
  • 59
  • please check this link here i given complete code of recording [Record,save,play & post to server][1] [1]: http://stackoverflow.com/questions/16754932/sending-avaudiorecorder-to-server-ios/16806407#16806407 – Kanhaiya Sharma Jun 05 '13 at 07:11
  • Thanks for quick reply. I am expecting simultaneous playing and recording with respect to earphone for recording and playing through iphone speaker. – ajay Jun 05 '13 at 07:20

1 Answers1

1

Set AVAudioSessionCategoryPlayAndRecord for playing those recording

Dushyant Singh
  • 721
  • 4
  • 13