{"id":686,"date":"2025-05-31T11:25:12","date_gmt":"2025-06-05T03:35:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=686"},"modified":"2025-06-05T11:25:12","modified_gmt":"2025-06-05T03:35:12","slug":"swift-%e8%aa%9e%e9%9f%b3%e8%bd%89%e6%96%87%e5%ad%97-%e9%8c%84%e9%9f%b3%e8%bd%89%e6%96%87%e5%ad%97-speech-kit","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e8%aa%9e%e9%9f%b3%e8%bd%89%e6%96%87%e5%ad%97-%e9%8c%84%e9%9f%b3%e8%bd%89%e6%96%87%e5%ad%97-speech-kit\/","title":{"rendered":"Swift \u8a9e\u97f3\u8f49\u6587\u5b57\u5be6\u4f5c\u6307\u5357\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h2>\u50cfSiri\u4e00\u6a23\u76e3\u807d\u8aaa\u8a71<\/h2>\n<h3>\u6253\u958b Speech \u6b0a\u9650<\/h3>\n<p>\u5728\u4f7f\u7528\u8a9e\u97f3\u8f49\u6587\u5b57\u529f\u80fd\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5728\u60a8\u7684\u61c9\u7528\u4e2d\u8acb\u6c42\u7528\u6236\u7684\u8a9e\u97f3\u8b58\u5225\u6b0a\u9650\u3002\u9019\u6b65\u9a5f\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u70ba\u5982\u679c\u7528\u6236\u62d2\u7d55\u6b0a\u9650\uff0c\u60a8\u7684\u61c9\u7528\u5c07\u7121\u6cd5\u6b63\u5e38\u904b\u884c\u8a9e\u97f3\u8b58\u5225\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/Swift-\u8a9e\u97f3\u8f49\u6587\u5b57-1.png\" alt=\"Swift \u8a9e\u97f3\u8f49\u6587\u5b57\u6b0a\u9650\u8acb\u6c42\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u5275\u5efa Button \u548c TextView<\/h3>\n<p>\u5728\u9019\u4e00\u90e8\u5206\uff0c\u6211\u5011\u5c07\u5275\u5efa\u4e00\u500b\u6309\u9215\u548c\u4e00\u500b\u6587\u672c\u8996\u5716\uff0c\u6309\u9215\u7528\u65bc\u958b\u59cb\u9304\u97f3\uff0c\u6587\u672c\u8996\u5716\u5247\u7528\u65bc\u986f\u793a\u8f49\u63db\u5f8c\u7684\u6587\u5b57\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/Swift-\u8a9e\u97f3\u8f49\u6587\u5b57-2.png\" alt=\"Swift \u8a9e\u97f3\u8f49\u6587\u5b57\u6309\u9215\u8207\u6587\u672c\u8996\u5716\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u6aa2\u67e5\u7528\u6236\u6b0a\u9650<\/h3>\n<p>\u4ee5\u4e0b\u4ee3\u78bc\u7528\u65bc\u8acb\u6c42\u8a9e\u97f3\u8b58\u5225\u7684\u6388\u6b0a\u4e26\u6aa2\u67e5\u7528\u6236\u7684\u6388\u6b0a\u72c0\u614b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nSFSpeechRecognizer.requestAuthorization { (authStatus) in<br \/>\n    var isButtonEnabled = false<\/p>\n<p>    switch authStatus {<br \/>\n    case .authorized:<br \/>\n        isButtonEnabled = true<\/p>\n<p>    case .denied:<br \/>\n        isButtonEnabled = false<br \/>\n        print(&#8220;\u7528\u6236\u62d2\u7d55\u63a5\u53d7\u8a9e\u97f3\u8b58\u5225&#8221;)<\/p>\n<p>    case .restricted:<br \/>\n        isButtonEnabled = false<br \/>\n        print(&#8220;\u8a9e\u97f3\u8b58\u5225\u529f\u80fd\u6c92\u6709\u7d93\u904e\u8a8d\u53ef&#8221;)<\/p>\n<p>    case .notDetermined:<br \/>\n        isButtonEnabled = false<br \/>\n        print(&#8220;\u7576\u524d\u8a2d\u5099\u4e0d\u80fd\u8a9e\u97f3\u8b58\u5225&#8221;)<\/p>\n<p>    @unknown default:<br \/>\n        print(&#8220;\u932f\u8aa4&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>AVAudioSessionMode<\/h3>\n<p>\u5728\u8a2d\u5b9a\u97f3\u983b\u6703\u8a71\u6a21\u5f0f\u6642\uff0c\u6839\u64da\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u9078\u64c7\u5408\u9069\u7684\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u97f3\u983b\u6703\u8a71\u6a21\u5f0f\u53ca\u5176\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>AudioSessionMode<\/th>\n<th>\u517c\u5bb9\u985e\u5225<\/th>\n<th>\u61c9\u7528\u5834\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Default<\/td>\n<td>All Category<\/td>\n<td>\u9ed8\u8a8d\u97f3\u983b\u6703\u8a71\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>VoiceChat<\/td>\n<td>PlayAndRecord<\/td>\n<td>VoIP(\u96d9\u5411\u8a9e\u97f3\u901a\u4fe1)<\/td>\n<\/tr>\n<tr>\n<td>GameChat<\/td>\n<td>PlayAndRecord<\/td>\n<td>\u904a\u6232\u9304\u5236(Game Kit)<\/td>\n<\/tr>\n<tr>\n<td>VideoRecording<\/td>\n<td>Record\u3001PlayAndRecord<\/td>\n<td>\u9304\u5236\u8996\u983b<\/td>\n<\/tr>\n<tr>\n<td>Measurement<\/td>\n<td>Playback\u3001Record\u3001PlayAndRecord<\/td>\n<td>\u97f3\u983b\u8f38\u5165\u6216\u8f38\u51fa\u7684\u6e2c\u91cf<\/td>\n<\/tr>\n<tr>\n<td>MoviePlayback<\/td>\n<td>Playback<\/td>\n<td>\u8996\u983b\u64ad\u653e<\/td>\n<\/tr>\n<tr>\n<td>VideoChat<\/td>\n<td>PlayAndRecord<\/td>\n<td>\u8996\u983b\u901a\u8a71<\/td>\n<\/tr>\n<tr>\n<td>SpokenAudio<\/td>\n<td>Playback\u3001SoloAmbient\u3001PlayAndRecord\u3001MultiRoute<\/td>\n<td>\u6709\u8072\u8b80\u7269<\/td>\n<\/tr>\n<tr>\n<td>VoicePrompt<\/td>\n<td>\u2013<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u958b\u59cb\u9304\u97f3<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u64b0\u5beb\u4e00\u500b\u51fd\u6578\u4f86\u958b\u59cb\u9304\u97f3\u4e26\u9032\u884c\u8a9e\u97f3\u8b58\u5225\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc startRecording() {<br \/>\n    if recognitionTask != nil {<br \/>\n        recognitionTask?.cancel()<br \/>\n        recognitionTask = nil<br \/>\n    }<\/p>\n<p>    let audioSession = AVAudioSession.sharedInstance()<br \/>\n    do {<br \/>\n        try audioSession.setCategory(AVAudioSession.Category.record)<br \/>\n        try audioSession.setMode(AVAudioSession.Mode.measurement)  \/\/ \u97f3\u983b\u8f38\u5165\u6216\u8f38\u51fa\u7684\u6e2c\u91cf<br \/>\n        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)  \/\/ \u505c\u6b62\u5176\u4ed6\u97f3\u6a02<br \/>\n    } catch {<br \/>\n        print(&#8220;audioSession properties weren&#8217;t set because of an error.&#8221;)<br \/>\n    }<\/p>\n<p>    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()<br \/>\n    let inputNode = audioEngine.inputNode <\/p>\n<p>    guard let recognitionRequest = recognitionRequest else {<br \/>\n        fatalError(&#8220;Unable to create an SFSpeechAudioBufferRecognitionRequest object&#8221;)<br \/>\n    }<\/p>\n<p>    recognitionRequest.shouldReportPartialResults = true<br \/>\n    recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in<br \/>\n        var isFinal = false<\/p>\n<p>        if result != nil {<br \/>\n            print(result?.bestTranscription.formattedString ?? &#8220;&#8221;)<br \/>\n            self.textView.text = result?.bestTranscription.formattedString<br \/>\n            isFinal = (result?.isFinal)!<br \/>\n        }<\/p>\n<p>        if error != nil || isFinal {<br \/>\n            self.audioEngine.stop()<br \/>\n            inputNode.removeTap(onBus: 0)<\/p>\n<p>            self.recognitionRequest = nil<br \/>\n            self.recognitionTask = nil<\/p>\n<p>            self.microphoneButton.isEnabled = true<br \/>\n        }<br \/>\n    })<\/p>\n<p>    let recordingFormat = inputNode.outputFormat(forBus: 0)<br \/>\n    inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in<br \/>\n        self.recognitionRequest?.append(buffer)<br \/>\n    }<\/p>\n<p>    audioEngine.prepare()<\/p>\n<p>    do {<br \/>\n        try audioEngine.start()<br \/>\n    } catch {<br \/>\n        print(&#8220;\u932f\u8aa4\uff0caudioEngine\u7121\u6cd5\u555f\u52d5\u3002&#8221;)<br \/>\n    }<\/p>\n<p>    textView.text = &#8220;\u8aaa\u9ede\u4ec0\u9ebc\uff0c\u6211\u5728\u807d\uff01&#8221;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u793a\u7bc4<\/h3>\n<p>\u9019\u88e1\u662f\u4e00\u500b\u8a9e\u97f3\u8f49\u6587\u5b57\u529f\u80fd\u7684\u793a\u7bc4\uff0c\u60a8\u53ef\u4ee5\u900f\u904e\u6b64\u52d5\u756b\u4e86\u89e3\u529f\u80fd\u7684\u904b\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/JPUKxP2.gif\" alt=\"Swift \u8a9e\u97f3\u8f49\u6587\u5b57\u793a\u7bc4\u52d5\u756b\" title=\"\"><\/p>\n<h3>GitHub \u8cc7\u6e90<\/h3>\n<p>\u5982\u679c\u60a8\u60f3\u6df1\u5165\u4e86\u89e3\u6216\u67e5\u770b\u5b8c\u6574\u4ee3\u78bc\uff0c\u8acb\u8a2a\u554f\u4ee5\u4e0b GitHub \u9801\u9762\uff1a[Swift Speech GitHub Repository](https:\/\/github.com\/Bgihe\/Swift-Speech)<\/p>\n<div style=\"font-family: 'Times New Roman'; text-align: left;\">\n    <span style=\"font-size: x-large;\"><br \/>\n        <b style=\"background-color: white;\">\u66f4\u591a Swift \u6587\u7ae0<\/b><br \/>\n    <\/span>\n<\/div>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-340\/\" title=\"Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5 \ud83d\udca5\" rel=\"internal\">Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5 \ud83d\udca5<\/a><\/p>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-8\/\" title=\"Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411 \ud83d\udcf1\" rel=\"internal\">Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411 \ud83d\udcf1<\/a><\/p>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-6\/\" title=\"Swift Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udd25\" rel=\"internal\">Swift Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udd25<\/a><\/p>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-4\/\" title=\"Swift UISegmentedControl \ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01\" rel=\"internal\">Swift UISegmentedControl \ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01<\/a><\/p>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-5\/\" title=\"Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c \ud83e\uddf9\" rel=\"internal\">Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c \ud83e\uddf9<\/a><\/p>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<p>**Q1: \u5982\u4f55\u8acb\u6c42\u8a9e\u97f3\u8b58\u5225\u6b0a\u9650\uff1f**<br \/>\nA1: \u4f7f\u7528 `SFSpeechRecognizer.requestAuthorization` \u65b9\u6cd5\u8acb\u6c42\u7528\u6236\u6388\u6b0a\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u6388\u6b0a\u72c0\u614b\u555f\u7528\u6216\u7981\u7528\u9304\u97f3\u6309\u9215\u3002<\/p>\n<p>**Q2: \u5982\u679c\u8a9e\u97f3\u8b58\u5225\u5931\u6557\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f**<br \/>\nA2: \u5982\u679c\u8a9e\u97f3\u8b58\u5225\u5931\u6557\uff0c\u60a8\u53ef\u4ee5\u6aa2\u67e5\u932f\u8aa4\u4fe1\u606f\u4e26\u63d0\u793a\u7528\u6236\u91cd\u8a66\uff0c\u6216\u63d0\u4f9b\u76f8\u61c9\u7684\u53cd\u994b\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728\u4e0d\u540c\u5834\u666f\u4e2d\u9078\u64c7 AVAudioSessionMode\uff1f**<br \/>\nA3: \u6839\u64da\u60a8\u7684\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684 AudioSessionMode\uff0c\u4f8b\u5982\u4f7f\u7528 `measurement` \u6a21\u5f0f\u9032\u884c\u9304\u97f3\u6216\u97f3\u983b\u6e2c\u91cf\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u512a\u5316\u4e0d\u50c5\u4f7f\u5167\u5bb9\u66f4\u5177\u5438\u5f15\u529b\uff0c\u4e5f\u7b26\u5408 Google SEO \u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u589e\u52a0\u6587\u7ae0\u7684\u53ef\u898b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u50cfSiri\u4e00\u6a23\u76e3\u807d\u8aaa\u8a71 \u6253\u958b Speech \u6b0a\u9650 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-686","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/Swift-\u8a9e\u97f3\u8f49\u6587\u5b57-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-b4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=686"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":7577,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/686\/revisions\/7577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}