0
A
回答
0
敲击键盘上您可以使用语音工具框架,使用Siri的语音识别。 先导入语音工具框架然后确认一个委托 这里是快捷版本。它可能有助于
import Speech
class ViewController: UIViewController, SFSpeechRecognizerDelegate {
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private let audioEngine = AVAudioEngine()
private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
override func viewDidLoad() {
super.viewDidLoad()
self.authorizeSpeech()
}
private func authorizeSpeech() {
SFSpeechRecognizer.requestAuthorization { (authStatus) in //4
var isButtonEnabled = false
switch authStatus { //5
case .authorized:
isButtonEnabled = true
case .denied:
isButtonEnabled = false
print("User denied access to speech recognition")
case .restricted:
isButtonEnabled = false
print("Speech recognition restricted on this device")
case .notDetermined:
isButtonEnabled = false
print("Speech recognition not yet authorized")
}
OperationQueue.main.addOperation() {
print(isButtonEnabled) //this tells that speech authorized or not
}
}
}
}
现在在你添加一些自定义消息的info.plist
<key>NSMicrophoneUsageDescription</key> <string>Your microphone will be used to record your speech when you press the Start Recording button.</string>
<key>NSSpeechRecognitionUsageDescription</key> <string>Speech recognition will be used to determine which words you speak into this device microphone.</string>
现在创建)
func startRecording() {
if recognitionTask != nil {
recognitionTask?.cancel()
recognitionTask = nil
}
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}
guard let recognitionRequest = recognitionRequest else {
fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
}
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
var isFinal = false
if result != nil {
your_text_view.text = result?.bestTranscription.formattedString
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)
self.recognitionRequest = nil
self.recognitionTask = nil
}
})
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("audioEngine couldn't start because of an error.")
}
}
确认委托
所谓的startRecording(新功能func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
if available {
startRecording()
} else {
//print("not implement")
}
}
+0
@thanks Umesh Verma,我想我的客观的C版本,请你分享它对我来说是非常有用的 – Nithya
相关问题
- 1. iOS xcode麦克风音量检测?
- 2. 如何监听麦克风的噪音?
- 3. 如何检测与Unity和IOS在麦克风的打击
- 4. 检测麦克风是否开启
- 5. IOS录音,如何在麦克风播放忙碌前检查麦克风/播放是否正常
- 6. 带麦克风的Android哨音检测
- 7. 我们可以在按钮上点击或打开键盘麦克风功能来获取输入吗?
- 8. 检测到麦克风的一击
- 9. 如何检测新麦克风是否可用?
- 10. 如何检测麦克风是否存在
- 11. 如何在Delphi 7中收听麦克风并检测声音响度
- 12. 如何检测iPad用户点击键盘隐藏按钮?
- 13. BASIC - 在按键上激活麦克风录音?
- 14. 检测麦克风/耳机?
- 15. 如何在模拟器上测试iOS麦克风的使用?
- 16. 检测点击并保持在iOS键盘按键
- 17. 语音语音识别android删除谷歌屏幕和麦克风按钮
- 18. C#在麦克风上聆听
- 19. 用于从音量调节麦克风获取语音的Api
- 20. 如何检测外部麦克风是否插入(实时)在Android上?
- 21. 如何禁用Xamarin表单中的键盘上的语音转文本按钮(听写按钮)?
- 22. 检测麦克风一击并做点什么
- 23. 麦克风音高/频率检测(尤其是动作3.0)
- 24. 在Windows 7上静音麦克风
- 25. 如何在语音识别完成后禁用麦克风?
- 26. HTML5语音输入麦克风接入
- 27. 如何检测Android设备麦克风中的打击
- 28. 当通话,麦克风或位置标语弹出时检测到 - iOS Swift
- 29. 如何检测OS X上的麦克风使用情况?
- 30. 如何检测用户是否点击刷新按钮或F5或Ctrl + F5?
[ios:如何检测语音听写是否用于UITextField?或麦克风按钮被轻敲在键盘上](http://stackoverflow.com/questions/32652775/ios-how-to-detect-if-voice-dictation-was-used-for-uitextfield-or-microphone-bu) – Saavaj
@ Saavaj,请你详细的看我的问题,我提到UITextview不是UITextField.UITextView是不同于UITextField – Nithya