{"id":298567,"date":"2020-02-12T09:00:31","date_gmt":"2020-02-12T09:00:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=298567"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-02-12T09:00:31","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=298567","title":{"rendered":"\u041f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442 \u043d\u0430 iOS \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Speech framework"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/487890\/\">\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442 \u0438 \u043f\u043e\u043a\u0430\u0436\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435. \u0421\u043d\u0430\u0447\u0430\u043ba \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f <i>iOS<\/i> \u0438 <i>Xcode<\/i>. \u0414\u043b\u044f <i>Speech Framework<\/i> \u043d\u0443\u0436\u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0443\u043c <i>iOS 10<\/i>. \u042f \u0441\u043e\u0437\u0434\u0430\u043b \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 <i>SwiftUI<\/i>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u0430 <i>iOS 13<\/i>. \u042d\u0442\u043e \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>Storyboard<\/i>. <\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 SwiftUI \u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043e\u0431\u0437\u043e\u0440, \u0442\u043e \u0432\u0430\u043c <a href=\"https:\/\/habr.com\/ru\/post\/483316\/\">\u0441\u044e\u0434\u0430<\/a>.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 <i>\u201cFile &gt; New &gt; Project\u2026\u201d<\/i>, \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 <i>\u201cSingle View App\u201d<\/i> \u0438 <i>\u201cUser Interface: SwiftUI\u201d<\/i>. \u041f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/re\/wt\/-o\/rewt-oyu3v_q0iesn9xc14i21vg.png\"><\/p>\n<p>  \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0430\u0439\u043b <i>ContentView.swift<\/i> \u0438 \u043c\u0435\u043d\u044f\u0435\u043c <i>\u00abstruct ContentView&#8230;\u00bb<\/i> \u043d\u0430:<\/p>\n<pre><code class=\"swift\">struct ContentView: View {     @ObservedObject var speechRec = SpeechRec()     var body: some View {         Text(speechRec.recognizedText)             .onAppear {                 self.speechRec.start()         }     } }  class SpeechRec: ObservableObject {     @Published private(set) var recognizedText = &quot;&quot;     func start() {         recognizedText = &quot;\u041f\u0440\u0438\u0432\u0435\u0442!&quot;     } } <\/code><\/pre>\n<p>  <i>ContentView<\/i> \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435. <i>SpeechRec<\/i> \u2014 \u044d\u0442\u043e \u0433\u0434\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442. \u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0430 <i>recognizedText<\/i>, \u0430 <i>ContentView<\/i> \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u0420\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0444\u0430\u0439\u043b <i>Info.plist<\/i> \u0438 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0442\u0443\u0434\u0430 \u0434\u0432\u0430 \u043a\u043b\u044e\u0447\u0430: <i>NSSpeechRecognitionUsageDescription<\/i> \u0438 <i>NSMicrophoneUsageDescription<\/i>. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/wc\/kc\/dc\/wckcdcrse6kp1qbt125ujpiubdw.png\"><\/p>\n<p>  \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c <i>Speech<\/i> \u0438 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"swift\">import Speech ... class SpeechRec: ObservableObject {     ...     func start() {         SFSpeechRecognizer.requestAuthorization { status in                      }     } } <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u0431\u0443\u0434\u0435\u0442 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/kr\/fc\/ad\/krfcad_nt4ctrogsmlucvvi2g7a.png\"><br \/>  .<br \/>  <cut><\/cut>  <\/p>\n<h2>\u041f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442<\/h2>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>SFSpeechRecognizer<\/i> \u0441 \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 <i>\u00abru-Ru\u00bb<\/i>, \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0440\u0443\u0441\u0441\u043a\u043e\u0439 \u0440\u0435\u0447\u0438. \u041f\u043e\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043e\u0442 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430.<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"swift\">...  class SpeechRec: ObservableObject {     @Published private(set) var recognizedText = &quot;&quot;          let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: &quot;ru-RU&quot;))     var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?     var recognitionTask: SFSpeechRecognitionTask?     let audioEngine = AVAudioEngine()          func start() {         self.recognizedText = &quot;\u0421\u043b\u0443\u0448\u0430\u044e...&quot;         SFSpeechRecognizer.requestAuthorization { status in             self.startRecognition()         }     }          func startRecognition() {         do {             recognitionRequest = SFSpeechAudioBufferRecognitionRequest()             guard let recognitionRequest = recognitionRequest else { return }                          recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest) { result, error in                 if let result = result {                     self.recognizedText = result.bestTranscription.formattedString                 }             }                          let recordingFormat = audioEngine.inputNode.outputFormat(forBus: 0)             audioEngine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in                 recognitionRequest.append(buffer)             }                          audioEngine.prepare()             try audioEngine.start()         }                  catch {                      }     } } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u043f\u0440\u043e\u0441\u0438\u043b\u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043d\u0430\u0447\u0430\u043b\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f. \u042f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0440\u0430\u0442\u043a\u043e \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u0438 \u0441\u043a\u0430\u0436\u0438\u0442\u0435 \u0447\u0442\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c, \u0432\u0430\u0448\u0430 \u0440\u0435\u0447\u044c \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bk\/6i\/hy\/bk6ihy-sqwhpy1x9x7vqmvzosw0.png\"><br \/>  .<br \/>  <cut><\/cut>  <\/p>\n<h2>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h2>\n<p>  \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0437\u0434\u0435\u0441\u044c:<br \/>  <a href=\"https:\/\/developer.apple.com\/documentation\/speech\" rel=\"nofollow\">developer.apple.com\/documentation\/speech<\/a><\/p>\n<p>  WWDC \u0432\u0438\u0434\u0435\u043e \u043f\u0440\u043e Speech framework:<br \/>  <a href=\"https:\/\/developer.apple.com\/videos\/all-videos\/?q=Speech\" rel=\"nofollow\">developer.apple.com\/videos\/all-videos\/?q=Speech<\/a><\/p>\n<p>  \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 github \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0434\u0435\u0441\u044c:<br \/>  <a href=\"https:\/\/github.com\/usenbekov\/speech-to-text-demo\" rel=\"nofollow\">github.com\/usenbekov\/speech-to-text-demo<\/a><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/487890\/\"> https:\/\/habr.com\/ru\/post\/487890\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/487890\/\">\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442 \u0438 \u043f\u043e\u043a\u0430\u0436\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435. \u0421\u043d\u0430\u0447\u0430\u043ba \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0432\u0435\u0440\u0441\u0438\u044f <i>iOS<\/i> \u0438 <i>Xcode<\/i>. \u0414\u043b\u044f <i>Speech Framework<\/i> \u043d\u0443\u0436\u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0443\u043c <i>iOS 10<\/i>. \u042f \u0441\u043e\u0437\u0434\u0430\u043b \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 <i>SwiftUI<\/i>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u0430 <i>iOS 13<\/i>. \u042d\u0442\u043e \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>Storyboard<\/i>. <\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 SwiftUI \u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043e\u0431\u0437\u043e\u0440, \u0442\u043e \u0432\u0430\u043c <a href=\"https:\/\/habr.com\/ru\/post\/483316\/\">\u0441\u044e\u0434\u0430<\/a>.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 <i>\u201cFile &gt; New &gt; Project\u2026\u201d<\/i>, \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 <i>\u201cSingle View App\u201d<\/i> \u0438 <i>\u201cUser Interface: SwiftUI\u201d<\/i>. \u041f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/re\/wt\/-o\/rewt-oyu3v_q0iesn9xc14i21vg.png\"><\/p>\n<p>  \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0430\u0439\u043b <i>ContentView.swift<\/i> \u0438 \u043c\u0435\u043d\u044f\u0435\u043c <i>\u00abstruct ContentView&#8230;\u00bb<\/i> \u043d\u0430:<\/p>\n<pre><code class=\"swift\">struct ContentView: View {     @ObservedObject var speechRec = SpeechRec()     var body: some View {         Text(speechRec.recognizedText)             .onAppear {                 self.speechRec.start()         }     } }  class SpeechRec: ObservableObject {     @Published private(set) var recognizedText = &quot;&quot;     func start() {         recognizedText = &quot;\u041f\u0440\u0438\u0432\u0435\u0442!&quot;     } } <\/code><\/pre>\n<p>  <i>ContentView<\/i> \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435. <i>SpeechRec<\/i> \u2014 \u044d\u0442\u043e \u0433\u0434\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0440\u0435\u0447\u044c \u0432 \u0442\u0435\u043a\u0441\u0442. \u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0430 <i>recognizedText<\/i>, \u0430 <i>ContentView<\/i> \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-298567","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=298567"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298567\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}