Question

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];
Was it helpful?

Solution

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);

OTHER TIPS

Using latest SDK it will look like this:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

Swift code:

    let recordingSession:AVAudioSession = AVAudioSession.sharedInstance()
    try! recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:.DefaultToSpeaker)

use this for record session:

let session = AVAudioSession.sharedInstance()
try! session.setCategory(
       AVAudioSessionCategoryPlayAndRecord,
       withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top