I came across a Speech framework when I was looking through apple’s official documentation. I had never seen this framework before, so I decided to look into it. The Speech API allows us to extend and enhance the Speech recognition experience in our applications without the need for a keyboard to do things like enter Speech. Since sensitive data such as voice is involved, it must be authorized by the user, that is, the info.plist file needs to add the key value of the response to obtain the user permission. Because speech recognition is not done locally on iOS devices, but on Apple servers. All voice data needs to be sent to Apple’s back-end servers for processing. Therefore, the user’s authorization must be obtained.
The < key > NSMicrophoneUsageDescription < / key > < string > when you press the start button, We will be using the microphone recording < / string > < key > NSSpeechRecognitionUsageDescription < / key > < string > when you speak to the microphone, we will begin to speech recognition < / string >Copy the code
Speech recognition is a web-based service, so restrictions are enforced so that all applications can use the service for free. Each device may limit the number of identifications that can be performed per day, and each application may be limited globally based on the number of requests per day. For example, if the recognition request fails quickly (within a second or two of starting), the recognition service may not be applied to your application for a while, and you may want to request the user again later. In addition, speech recognition can place a relatively high burden on battery life and network usage. In iOS 10, voice audio duration is limited to about 1 minute, which is similar to the limitation of keyboard-related dictation. The official case study for the use of Speech posture can be found here. Of course, you can also watch my small Demo, welcome Star encourage.
Here’s my code and some code comments:
// // viewController.Swift // YaHoYiSpeech // // Created by Sun Online YHY on 2017/6/10. // Copyright © 2017 Sun Online. All rights reserved. // import UIKit import Speech class ViewController: UIViewController { @IBOutlet weak var textView: UITextView! @IBOutlet weak var startBtn: UIButton! // Before voice recognition can be performed, you must obtain the appropriate authorization from the user, because voice recognition is not performed locally on the iOS device, but on the Apple server. All voice data needs to be sent to Apple's back-end servers for processing. Therefore, the user's authorization must be obtained. // Create a speech recognizer that specifies the locale for speech recognition, which language will be converted in the futureletspeechRecognizer = SFSpeechRecognizer(locale: Locale. AutoupdatingCurrent) / / using the identifier in Taiwan area is set here, the future will be converted into traditional Chinese / / privatelet speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "zh-TW") // Initiates a speech recognition request specifying an audio input source for the speech recognizer, in this case the recognized speech provided in the audio buffer. / / except SFSpeechAudioBufferRecognitionRequest include: / / SFSpeechRecognitionRequest voice recognition request from audio sources. / / SFSpeechURLRecognitionRequest at the request of voice recognition of the recorded audio file. private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest? // Voice recognition task, can monitor the recognition progress. It allows you to cancel or terminate the current speech recognitionTask. // Voice engine, responsible for providing recording input privatelet audioEngine = AVAudioEngine()
override func viewDidLoad() {super.viewdidload () // print out the supported area of the speech recognizer, which is the identifier required above for the locale when initializing SFSpeechRecognizerprint(SFSpeechRecognizer.supportedLocales())
startBtn.isEnabled = false// set up speechRecognizer agent? .delegate = self // Ask the user to grant your application permission to perform speech recognition. SFSpeechRecognizer.requestAuthorization { (status)in
var isButtonenable = false// Authorization status of the recognizer switch status {// Authorizedcase.authorized: // Authorized to allow recording button click isButtonenable =true// Deny authorizationcase .denied:
isButtonenable = false
print("User denied access to speech recognition") // Confidential, i.e. not authorizedcase .restricted:
isButtonenable = false
print("Speech recognition restricted on this device") // Undecidedcase .notDetermined:
isButtonenable = false
print("Speech recognition not yet authorized")
}
OperationQueue.main.addOperation() {
self.startBtn.isEnabled = isButtonenable
}
}
}
func startRecordingPersonSpeech() {// Check whether the recognitionTask task is running. If yes, cancel the task and start a new oneifrecognitionTask ! = nil {// Cancel current speech recognition task. recognitionTask? .cancel() // The current status of the speech recognition task is an enumerated valueprint(recognitionTask! .state) recognitionTask = nil} // Create an AVAudioSession for recordinglet audioSession = AVAudioSession.sharedInstance()
do{/ / category is set to record, recording the try audioSession. SetCategory (AVAudioSessionCategoryRecord) / / mode is set to the measurement of the try AudioSession. SetMode (AVAudioSessionModeMeasurement) / / open audioSession try audioSession. SetActive (true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error."} // Initialize RecognitionRequest, Behind we will use it will forward the recording data to apple server recognitionRequest = SFSpeechAudioBufferRecognitionRequest () / / check if the iPhone was effective guard recording equipmentlet inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}
//
guard let recognitionRequest = recognitionRequest else {
fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")} / / the user talk at the same time, will return to recognitionRequest. The recognition results in batches shouldReportPartialResults =true// Start the recognition using the recognitionTask method. recognitionTask = speechRecognizer? .recognitionTask(with: recognitionRequest, resultHandler: { (result, error)inVar isFinal =false// If result is not nil,ifresult ! Textview. text = result? Self.textview. text = result? . The bestTranscription. FormattedString / / if the result is in the end, sets the isFinal totrueisFinal = (result? .isFinal)! } // If no error occurs, or if result ends, stop audioEngine recording, and stop recognitionRequest and recognitionTaskiferror ! = nil || isFinal { self.audioEngine.stop() inputNode.removeTap(onBus: 0) self.recognitionRequest = nil self.recognitionTask = nil // Start recording button available self.startbtn.isEnabled =true}}) // Add an audio input to recognitionRequestlet recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) inself.recognitionRequest? .append(buffer) } audioEngine.prepare()doTry audioengine.start ()} catch {// start audioengine.start ()} catch {print("audioEngine couldn't start because of an error.")
}
textView.text = "Go ahead!
}
@IBAction func startRecording(_ sender: UIButton) {
ifAudioengine.isrunning {// Stops the recording Audioengine.stop () // indicates that the audio source is complete and no longer attached to the recognition request. recognitionRequest? .endAudio() startBtn.isEnabled =false
startBtn.setTitle("Start".for: .normal)
} else {
startRecordingPersonSpeech()
startBtn.setTitle("The end".for: .normal)
}
}
}
extension ViewController: SFSpeechRecognizerDelegate {
func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
if available {
startBtn.isEnabled = true
}else {
startBtn.isEnabled = false}}}Copy the code