Jim*_*ery 7 audio ios avaudiosession swift
我在 Swift 中构建了一个 iOS 应用程序来记录音频剪辑,然后将这些剪辑发送到服务器。我做的每一个录音都很安静。
最初我认为我的问题类似于 Stack Overflow 上的这个问题 - 但是在尝试了这个解决方案之后,我的录音仍然很安静。
通过扬声器路由音频不会使录音变得更响亮,如下所示:
请注意,它不是在有问题的设备上播放。问题是录音太安静了。我已经测试了麦克风本身,它很好。
这是我能产生的最好的波形,这几乎是对着麦克风大喊大叫。可以看到,记录的波形非常安静:

有没有办法让我的 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)
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
录制音频时,将音频会话设置为AVAudioSessionCategoryPlayAndRecord或只是AVAudioSessionCategoryRecord。完成后,将其重新设置为AVAudioSessionCategoryPlayback。
| 归档时间: |
|
| 查看次数: |
2887 次 |
| 最近记录: |