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之间,那对我来说就好了。 –