簡體   English   中英

在IOS中使用AVAudioRecorder錄制聲音時音量下降

[英]Volume is going down while recording sound with AVAudioRecorder in IOS

我正在開發一個應用程序,我用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];

好的,你需要做更多的事情來實現這一目標。 首先,進口

 #import <AudioToolbox/AudioServices.h>

然后,在viewDidLoad方法中。 把這段代碼

 [[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代碼:

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

用它來記錄會話:

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