как определить, использовалась ли голосовая диктовка для UITextView? Или была нажата кнопка микрофона на клавиатуре в текстовом окне пользовательского интерфейса
ios: как определить, использовалась ли голосовая диктовка для UITextView? Или была нажата кнопка микрофона на клавиатуре
comment
Возможный дубликат ios : как определить, использовалась ли голосовая диктовка для UITextField? Или была нажата кнопка микрофона на клавиатуре
- person Saavaj   schedule 27.12.2016
comment
@ Saavaj, Не могли бы вы подробно рассмотреть мой вопрос, я упомянул UITextview, а не UITextField. UITextView отличается от UITextField
- person Nithya   schedule 27.12.2016
Ответы (2)
Вы можете использовать структуру Speech Kit, которую 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>
теперь создайте новую функцию с именем startRecording ()
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.")
}
}
подтвердить делегата
func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
if available {
startRecording()
} else {
//print("not implement")
}
}
person
Umesh Verma
schedule
27.12.2016
@ спасибо Umesh Verma, я хочу объективную версию c, не могли бы вы поделиться, это будет очень полезно для меня
- person Nithya; 27.12.2016
Если вы ищете это для скрытия метки-заполнителя, вам поможет один из нижеприведенных.
public func textViewDidChange(_ textView: UITextView) {
if (textView.text?.isEmpty)! {
lblPlaceholder?.isHidden = false
} else {
lblPlaceholder?.isHidden = true
}
}
person
Murali
schedule
14.06.2020