Volume decreases when recording audio using AVAudioRecorder in iOS

I am developing an application in which I record sound using AVAudioRecorder with the .CAF format, everything is fine, but my problem is that when I play the recorded sound, then it plays in a very small amount. Somehow the sound volume will be minimal. I tried so many solutions, but did not find a solution for resolution, if anyone has any ideas, then please tell me that it is very important for me. Below is the snippet code

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];
+4
source share
4 answers

Well, you need to do a little more to achieve this. First, import

 #import <AudioToolbox/AudioServices.h>

viewDidLoad.

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
 AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
 sizeof (audioRouteOverride),&audioRouteOverride);
+8

SDK, :

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

:

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

:

let session = AVAudioSession.sharedInstance()
try! session.setCategory(
       AVAudioSessionCategoryPlayAndRecord,
       withOptions:AVAudioSessionCate goryOptions.DefaultToSpeaker)
-1
source

Source: https://habr.com/ru/post/1524299/


All Articles