Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volume is going down while recording sound with AVAudioRecorder in IOS

I am developing an app in which I am recording sound with AVAudioRecorder with .CAF format, everything is fine but my problem is that when I am playing the recorded sound then its playing in very low volume. Somehow The volume of sound is going to least level. Tried so many solution but didn't find a solution to resolution, if anybody has any idea then please tell me, its very important for me. Below is the code of snippet

AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [session setActive:YES error:nil];
        // Define the recorder setting
        recordSetting = [[NSMutableDictionary alloc] init];

        [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

        NSArray *pathComponents = [NSArray arrayWithObjects:
                                   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                   [NSString stringWithFormat:@"%@.caf",@"temp"],
                                   nil];
        NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
        recorder = [[AVAudioRecorder alloc]initWithURL:outputFileURL settings:recordSetting error:nil];
        recorder.delegate = self;
        recorder.meteringEnabled = YES;
        [recorder recordForDuration:5];
        [recorder prepareToRecord];
        [recorder record];
like image 983
rahul Avatar asked Jan 29 '14 15:01

rahul


1 Answers

Ok, you need to do bit more to achieve that. First, import

 #import <AudioToolbox/AudioServices.h>

then, inside your viewDidLoad method. put this code

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
 AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
 sizeof (audioRouteOverride),&audioRouteOverride);
like image 52
Thilina Chamath Hewagama Avatar answered Oct 10 '22 20:10

Thilina Chamath Hewagama