菜鸟教程小白 发表于 2022-12-12 09:33:20

ios - AVAudio 的最佳麦克风录音设置是什么?


                                            <p><p>所以我有当前的代码:</p>

<pre><code>- (void)viewDidLoad
{
    ;
    //for recording audio
    _stopButton.enabled = NO;
    _playButton.hidden = true;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(
                                                   NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths;

    NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@&#34;recording.caf&#34;];

    NSURL *soundFileURL = ;

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    , AVFormatIDKey,
                                    , AVSampleRateKey,
                                    , AVNumberOfChannelsKey,
                                    , AVSampleRateConverterAudioQualityKey,
                                    , AVEncoderBitRateKey,
                                    , AVEncoderBitDepthHintKey,
                                    nil];

    NSError *error = nil;

    AVAudioSession *audioSession = ;
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                        error:nil];

    _audioRecorder = [
                      initWithURL:soundFileURL
                      settings:recordSettings
                      error:&amp;error];

    if (error)
    {
      NSLog(@&#34;error: %@&#34;, );
    } else {
      ;
    }
    //----------------------------
    //end

}
</code></pre>

<hr/>

<p>还有玩家:</p>

<pre><code>- (IBAction)playAudio:(id)sender {
    ;
    if (!_audioRecorder.recording)
    {
      _stopButton.enabled = YES;
      _recordButton.enabled = NO;

      NSError *error;

      _audioPlayer = [
                        initWithContentsOfURL:_audioRecorder.url
                        error:&amp;error];
      _audioPlayer.volume = 5;
      _audioPlayer.delegate = self;

      if (error)
            NSLog(@&#34;Error: %@&#34;,
                  );
      else
            ;
    }else{
      NSLog(@&#34;errormax&#34;);
    }
}
</code></pre>

<p>我在 <code>5</code> 有 <code>_audioPlayer.volume</code>,因为再低一点,你就几乎看不到它了。但在那个音量下,它看起来和听起来都非常失真。为什么它听起来不像“语音备忘录”,它以合理的音量播放而不会失真?</p>

<p>什么是最好的质量设置?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我个人更喜欢 PCM 编码,我使用它的质量更好。</p>

<p>尝试:</p>

<pre><code>NSDictionary *recordSettings =
[NSDictionary dictionaryWithObjectsAndKeys:
, AVFormatIDKey,
, AVSampleRateKey,
, AVNumberOfChannelsKey,
, AVLinearPCMBitDepthKey,
, AVLinearPCMIsBigEndianKey,
, AVLinearPCMIsFloatKey,
nil];
</code></pre>

<p>或使其更具可读性:</p>

<pre><code>NSDictionary * recordSetting;
recordSetting = [ init];
forKey:AVFormatIDKey];
forKey:AVSampleRateKey];
forKey:AVNumberOfChannelsKey];
forKey:AVLinearPCMBitDepthKey];
forKey:AVLinearPCMIsBigEndianKey];
forKey:AVLinearPCMIsFloatKey];
</code></pre>

<p>像这样调整你的函数:</p>

<pre><code>- (IBAction)playAudio:(id)sender {
    ;
    if (!_audioRecorder.recording)
    {
      _stopButton.enabled = YES;
      _recordButton.enabled = NO;

      //force the session to play to come out of speakers!
      UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
      AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &amp;sessionCategory);
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;audioRouteOverride);

      NSError *error;

      _audioPlayer = [
                        initWithContentsOfURL:_audioRecorder.url
                        error:&amp;error];
      _audioPlayer.delegate = self;
      ;

      if (error)
            NSLog(@&#34;Error: %@&#34;,
                  );
      else
            ;
    }else{
      NSLog(@&#34;errormax&#34;);
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AVAudio 的最佳麦克风录音设置是什么?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23323928/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23323928/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AVAudio 的最佳麦克风录音设置是什么?