一旦用戶停止服用,我怎樣才能停止錄音? 像Siri。一旦你說,嗨Siri它會迴應你的聲音。意味着Siri應用程序正在收聽音頻,直到您停止服用。在AVAudioRecorder中尋找沉默會話
我正在嘗試做同樣的事情。如果我說,獲取天氣詳情一旦我停止了我的聲音。我想觸發一種方法或者用錄製的音頻調用API直到停止。
我的要求是應用程序應該不斷地聽用戶找到語音結束事件發送數據到服務器或者只是觸發一個方法。
代碼:
import UIKit
import CoreAudio
import CoreAudioKit
import AVFoundation
import Foundation
import AVKit
class ViewController: UIViewController, AVAudioRecorderDelegate {
private var recorder : AVAudioRecorder? = nil
private var isRecording : Bool = false
private var timer : Timer? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
permissionWasGranted { (isValied) in
print("isValied")
self.isRecording = false;
self.intiateTimer()
}
}
@objc func intiateTimer() {
self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateTimer), userInfo: nil, repeats: true)
}
@objc func updateTimer() {
if !isRecording {
//recorder = nil
self.initRecorder()
print("Recording intiated")
}
else {
print("Recording Started")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getDocumentsDirectory() -> URL {
let fileManager = FileManager.default
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = urls.first!
return documentDirectory.appendingPathComponent("recording.m4a")
}
// MARK: protocol
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
recorder.stop()
recorder.deleteRecording()
recorder.prepareToRecord()
isRecording = false
self.updateTimer()
}
func permissionWasGranted(result: @escaping (_: Bool)->()) {
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
//if IS_DEBUG { print("Permission granted") }
print("Permission granted")
result(true)
return
case AVAudioSessionRecordPermission.denied:
//if IS_DEBUG { print("Pemission denied") }
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
//if IS_DEBUG { print("Request permission here") }
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
result(true)
return
}
})
}
result(false)
}
func initRecorder() {
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
try session.setActive(true)
try recorder = AVAudioRecorder(url: getDocumentsDirectory(), settings: settings)
recorder!.delegate = self
recorder!.isMeteringEnabled = true
if !recorder!.prepareToRecord() {
print("Error: AVAudioRecorder prepareToRecord failed")
}
let decibels = self.getDispersyPercent()
if decibels > -120 && decibels < -20 {
self.timer?.invalidate()
isRecording = true;
self.start()
}
} catch {
print("Error: AVAudioRecorder creation failed")
}
}
func start() {
recorder?.record()
recorder?.updateMeters()
}
func update() {
if let recorder = recorder {
recorder.updateMeters()
}
}
func getDispersyPercent() -> Float {
if let recorder = recorder {
let decibels = recorder.averagePower(forChannel: 0)
return decibels
}
return 0
}
}
您需要跟蹤記錄 –
的分貝(dB)這個答案可以幫助你https://stackoverflow.com/a/43429136/468724 。請記住,您需要觸發定時器以在特定時間間隔後獲取值 –
您可以使用iOS sdk中的語音合成器。它會立即將您的聲音轉換爲文字。對於演示https://github.com/Gagan5278/SpeechSynthesizer –