gpt4 book ai didi

ios - Swift + AVAudioRecorder 录音很安静

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:49 27 4
gpt4 key购买 nike

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

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

通过扬声器路由音频不会使录音声音更大,如这里所建议的:

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

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

这是我能产生的最佳波形,这几乎是对着麦克风大喊大叫。可以看到,录制的波形非常安静:

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.

有什么方法可以让我的 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)

}
}
})
}

最佳答案

录制音频时,将 Audio Session 设置为AVAudioSessionCategoryPlayAndRecord 或只是AVAudioSessionCategoryRecord。完成后,将其设置回 AVAudioSessionCategoryPlayback

关于ios - Swift + AVAudioRecorder 录音很安静,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28070773/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com