Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift + AVAudioRecorder records very quietly

I have built an iOS App in Swift that records audio clips, these clips are then sent up to the server. Every recording I make is very quiet.

Initially I thought my problems were similar to this question on Stack Overflow - but after trying this solution, my recordings are still very quiet.

Routing the audio through the speaker does not make the recordings any louder, as is suggested here:

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

Note that its not playback on the device that is an issue. The issue is that the recordings are too quiet. I have tested the Microphone itself, and it is fine.

This is the best waveform I can produce, and this is almost shouting into the Microphone. As you can see, the waveform recorded is very quiet:

This is the best waveform I can produce, and this is almost shouting into the Microphone. As you can see, the waveform recorded is very quiet.

Is there any way of getting my iOS App to record at a louder volume?

This is the code I am using to record the audio clips:

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)

            }
        }
    })
}
like image 435
Jimmery Avatar asked Jan 21 '15 15:01

Jimmery


1 Answers

When recording audio, set the audio session to AVAudioSessionCategoryPlayAndRecord or just AVAudioSessionCategoryRecord. When you're done, set it back to just AVAudioSessionCategoryPlayback.

like image 195
icaksama Avatar answered Oct 16 '22 04:10

icaksama