【问题标题】:Swift + AVAudioRecorder records very quietlySwift + AVAudioRecorder 非常安静地录制
【发布时间】:2015-01-21 15:24:40
【问题描述】:

我在 Swift 中构建了一个 iOS 应用程序来记录音频剪辑,然后将这些剪辑发送到服务器。我录制的每一个录音都很安静。

最初我认为我的问题类似于 Stack Overflow 上的这个问题 - 但是在尝试了这个解决方案之后,我的录音仍然很安静。

通过扬声器路由音频不会使录音变得更响亮,如下所示:

https://stackoverflow.com/a/5662478/1037617

请注意,它不能在设备上播放,这是一个问题。问题是录音太安静了。我已经测试了麦克风本身,没问题。

这是我能产生的最好的波形,这几乎是对着麦克风大喊大叫。如您所见,记录的波形非常安静:

有什么方法可以让我的 iOS 应用以更大的音量录制?

这是我用来录制音频剪辑的代码:

let recorderSettings=[
    AVFormatIDKey            : kAudioFormatLinearPCM,
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey      : 128000,
    AVNumberOfChannelsKey    : 1,
    AVSampleRateKey          : 44100.0
]

let session: AVAudioSession = AVAudioSession.sharedInstance()
var error: NSError?
if session.respondsToSelector("requestRecordPermission:") {
    AVAudioSession.sharedInstance().requestRecordPermission( { (granted:Bool) -> Void in
        if !granted {
            println("permission not granted")
        }else{
            println("permission granted")

            if !session.setCategory(AVAudioSessionCategoryRecord, error: &error) { // also tried PlaybackAndRecord
                println("could not set sesssion category")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            if !session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: &error) {
                println("could not override output audio")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            if !session.setActive(true, error: &error) {
                println("could not make active")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            self.currentFilename = "xxxx.wav"
            let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
            let docsDir: AnyObject=dirPaths[0]
            self.recordedFilePath = docsDir.stringByAppendingPathComponent(self.currentFilename)
            self.recordedFileURL = NSURL(fileURLWithPath: self.recordedFilePath)

            self.recorder = AVAudioRecorder(URL:self.recordedFileURL, settings: self.recorderSettings, error: &error)

            if let e = error {
                println(e)
                println(e.localizedDescription)
                var err = NSError(domain: NSOSStatusErrorDomain, code: e.code, userInfo: nil)
                println(err.description)
            }else{

                self.recorder?.delegate = self
                self.recorder?.meteringEnabled = true
                self.recorder?.prepareToRecord()

                self.recorder?.record()

                self.meterTimer = NSTimer.scheduledTimerWithTimeInterval(0.1,
                    target: self,
                    selector: "updateRecordAudioMeter:",
                    userInfo: nil,
                    repeats: true)

            }
        }
    })
}

【问题讨论】:

  • 我可以看看你的刻录机设置吗?您是否也在不同的设备上尝试过此代码?也许你的设备麦克风坏了
  • @zellb 我已经添加了记录器设置。问题绝对不是麦克风 - 我已经尝试并测试了一些。
  • 对此有何更新/解决方案?我正在经历类似的事情?
  • 我也很想知道一个更好的解决方案。我已经尝试了下面给出的答案,但是录音量仍然很低。我将它与 Voice Memos 应用程序(用于录制的默认 iOS 应用程序)进行比较,它以某种方式录制或播放录音的声音更大。我想实现同样的目标。

标签: ios swift audio avaudiosession


【解决方案1】:

录制音频时,将音频会话设置为 AVAudioSessionCategoryPlayAndRecord 或只是 AVAudioSessionCategoryRecord。完成后,将其重新设置为 AVAudioSessionCategoryPlayback

【讨论】:

    【解决方案2】:

    录制后需要将AVAudioSession类别设置回AVAudioSessionCategoryPlayback

    【讨论】:

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