7
我試圖用AVAudioRecorder
錄製音頻。我需要建立這樣一個簡單的波形:iOS:創建簡單的音頻波形動畫
當用戶對着麥克風圓圈表示用戶的語音的水平。我試圖衡量這段代碼的聲音,但代碼未檢測到任何緩衝,而不是工作:
func levelTimerCallback(timer:Timer) {
//we have to update meters before we can get the metering values
audioRecorder.updateMeters()
//print to the console if we are beyond a threshold value. Here I've used -7
if audioRecorder.averagePower(forChannel: 1) > -7 {
print(" level I'm hearin' you in dat mic ")
print(audioRecorder.averagePower(forChannel: 0))
}
}
錄製音頻:
func startRecording() {
let audioSession = AVAudioSession.sharedInstance()
do {
// Audio Settings
settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
audioRecorder = try AVAudioRecorder(url: self.directoryURL(), settings: settings)
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
audioRecorder.isMeteringEnabled = true
} catch {
finishRecording(success: false)
}
do {
try audioSession.setActive(true)
audioRecorder.record()
TIMERRRRR = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(ViewController.levelTimerCallback), userInfo: nil, repeats: true)
} catch {
}
}
我添加的東西,我認爲是接近正確答案。 –
但你可以檢查編輯的代碼的一個問題? –
如果您的比例介於0和1之間,那對我來說就好了。 –