mercredi 21 janvier 2015

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:


http://ift.tt/1ANvrwS


Note that its not playback on the device that is an issue. The issue is that the recordings are too quiet. I have tried with a variety of microphones in both a Mac and a PC. All the microphones are much louder (sensitive to sound?) on the PC than the Mac.


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 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 try PlaybackAndRecord
println("could not set sesssion category")
if let e = error {
println(e.localizedDescription)
}
}

if !session.setActive(true, error: &error) {
println("could not make session active")
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)

}
}
})
}

Aucun commentaire:

Enregistrer un commentaire