【问题标题】:Volume is going down while recording sound with AVAudioRecorder in IOS在 IOS 中使用 AVAudioRecorder 录制声音时音量正在下降
【发布时间】:2014-02-21 11:37:02
【问题描述】:

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

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

【问题讨论】:

    标签: ios volume avaudiorecorder


    【解决方案1】:

    好的,你需要做更多的事情来实现这一点。一、导入

     #import <AudioToolbox/AudioServices.h>
    

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

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

    【讨论】:

    • 我刚试过这个并得到链接器错误。我正在为架构 i386 使用 xcode 5.1 未定义符号:“_AudioSessionSetProperty”,引用自:ViewController.o ld 中的 -[ViewController viewDidLoad]:未找到架构 i386 的符号 clang:错误:链接器命令失败,退出代码为 1 (使用 -v 查看调用)
    【解决方案2】:

    使用最新的 SDK 会如下所示:

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

    【讨论】:

      【解决方案3】:

      Swift 代码:

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

      【讨论】:

        【解决方案4】:

        将此用于录制会话:

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

        【讨论】:

          猜你喜欢
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 2011-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多