简体   繁体   English

在IOS中使用AVAudioRecorder录制声音时音量下降

[英]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. 我正在开发一个应用程序,我用AVAudioRecorder以.CAF格式录制声音,一切都很好,但我的问题是,当我播放录制的声音时,它的播放量非常低。 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];

Ok, you need to do bit more to achieve that. 好的,你需要做更多的事情来实现这一目标。 First, import 首先,进口

 #import <AudioToolbox/AudioServices.h>

then, inside your viewDidLoad method. 然后,在viewDidLoad方法中。 put this code 把这段代码

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

使用最新的SDK,它将如下所示:

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

Swift code: SWIFT代码:

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM