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

时间:2014-01-29 15:07:12

标签: ios volume avaudiorecorder

我正在开发一个应用程序,其中我用AVAudioRecorder以.CAF格式录制声音,一切都很好,但我的问题是,当我播放录制的声音时,它的播放音量非常低。不知何故声音的音量达到最低水平。尝试了这么多解决方案,但没有找到解决方案的解决方案,如果有人有任何想法那么请告诉我,这对我来说非常重要。 以下是代码段

的代码
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 个答案:

答案 0 :(得分:8)

好的,你需要做更多的事情才能做到这一点。首先,导入

 #import <AudioToolbox/AudioServices.h>

然后,在viewDidLoad方法中。把这段代码

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

答案 1 :(得分:6)

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

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

答案 2 :(得分:5)

Swift代码:

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

答案 3 :(得分:-1)

将此用于记录会话:

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