質問編集履歴

1

内容の修正

2019/08/01 06:32

投稿

Haruto513
Haruto513

スコア52

test CHANGED
File without changes
test CHANGED
@@ -60,136 +60,126 @@
60
60
 
61
61
  if let recognitionTask = recognitionTask {
62
62
 
63
- recognitionTask.cancel()
63
+ recognitionTask.cancel()
64
-
64
+
65
- self.recognitionTask = nil
65
+ self.recognitionTask = nil
66
+
67
+ }
68
+
69
+
70
+
71
+ let audioSession = AVAudioSession.sharedInstance()
72
+
73
+
74
+
75
+ try audioSession.setCategory(AVAudioSession.Category.record)
76
+
77
+ try audioSession.setMode(AVAudioSession.Mode.measurement)
78
+
79
+ try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
80
+
81
+
82
+
83
+ recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
84
+
85
+
86
+
87
+ let inputNode: AVAudioInputNode = audioEngine.inputNode
88
+
89
+ guard let Request = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest") }
90
+
91
+
92
+
93
+ recognitionRequest?.shouldReportPartialResults = true
94
+
95
+
96
+
97
+ recognitionTask = speechRecognizer.recognitionTask(with: Request, resultHandler: { result, error in
98
+
99
+
100
+
101
+ if let result = result {
102
+
103
+ self.recognizerImage.removeFromSuperview()
104
+
105
+ self.audioEngine.stop()
106
+
107
+ self.recognitionRequest?.endAudio()
108
+
109
+ inputNode.removeTap(onBus: 0)
110
+
111
+ self.recognitionRequest = nil
112
+
113
+
114
+
115
+ let word = result.bestTranscription.formattedString
116
+
117
+ //グローバル変数 recognizedWord
118
+
119
+ recognizedWord = word
120
+
121
+ let nextvc = self.storyboard?.instantiateViewController(withIdentifier: "nextvc") as! NextViewController
122
+
123
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
124
+
125
+ self.present(nextvc, animated: true, completion: nil)
66
126
 
67
127
  }
68
128
 
69
-
70
-
71
- let audioSession = AVAudioSession.sharedInstance()
129
+ }
72
-
73
-
74
-
75
- try audioSession.setCategory(AVAudioSession.Category.record)
130
+
76
-
77
- try audioSession.setMode(AVAudioSession.Mode.measurement)
131
+
78
-
79
- try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
132
+
80
-
81
-
82
-
83
- recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
84
-
85
-
86
-
87
- let inputNode: AVAudioInputNode = audioEngine.inputNode
88
-
89
- guard let Request = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest") }
90
-
91
-
92
-
93
- recognitionRequest?.shouldReportPartialResults = true
94
-
95
-
96
-
97
- recognitionTask = speechRecognizer.recognitionTask(with: Request, resultHandler: { result, error in
98
-
99
-
100
-
101
- if let result = result {
133
+ if error != nil {
102
-
134
+
103
- self.recognizerImage.removeFromSuperview()
135
+ print("エラー出力")
136
+
104
-
137
+ print(error!)
138
+
105
- self.audioEngine.stop()
139
+ self.audioEngine.stop()
106
-
140
+
107
- self.recognitionRequest?.endAudio()
141
+ self.recognitionRequest?.endAudio()
108
-
142
+
109
- inputNode.removeTap(onBus: 0)
143
+ inputNode.removeTap(onBus: 0)
110
-
144
+
111
- self.recognitionRequest = nil
145
+ self.recognitionRequest = nil
112
-
113
-
114
-
115
- let word = result.bestTranscription.formattedString
146
+
116
-
117
- //グローバル変数 recognizedWord
118
-
119
- recognizedWord = word
120
-
121
- let nextvc = self.storyboard?.instantiateViewController(withIdentifier: "nextvc") as! NextViewController
122
-
123
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
124
-
125
- self.present(nextvc, animated: true, completion: nil)
126
-
127
- }
128
-
129
- }
130
-
131
-
132
-
133
- if error != nil {
134
-
135
- print("エラー出力")
136
-
137
- print(error!)
138
-
139
- self.audioEngine.stop()
140
-
141
- self.recognitionRequest?.endAudio()
142
-
143
- inputNode.removeTap(onBus: 0)
144
-
145
- self.recognitionRequest = nil
146
-
147
- self.recognitionTask = nil
147
+ self.recognitionTask = nil
148
148
 
149
149
 
150
150
 
151
- self.recognizerImage.removeFromSuperview()
151
+ self.recognizerImage.removeFromSuperview()
152
-
152
+
153
- }
153
+ }
154
-
154
+
155
- })
155
+ })
156
-
157
-
158
-
156
+
157
+
158
+
159
- let recordingFormat = inputNode.outputFormat(forBus: 0)
159
+ let recordingFormat = inputNode.outputFormat(forBus: 0)
160
-
160
+
161
- inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat, block: { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
161
+ inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat, block: { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
162
-
162
+
163
- self.recognitionRequest?.append(buffer)
163
+ self.recognitionRequest?.append(buffer)
164
-
164
+
165
- })
165
+ })
166
-
167
-
168
-
166
+
167
+
168
+
169
- audioEngine.prepare()
169
+ audioEngine.prepare()
170
-
170
+
171
- try audioEngine.start()
171
+ try audioEngine.start()
172
-
173
-
174
-
172
+
173
+
174
+
175
- }
175
+ }
176
-
176
+
177
- }
177
+ }
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
178
 
187
179
 
188
180
 
189
181
  ```
190
182
 
191
-
192
-
193
183
  原因、解決法を知っている方がいらっしゃたら、教えて頂けると幸いです。
194
184
 
195
185
 
@@ -201,3 +191,67 @@
201
191
  Xcode10.2.1
202
192
 
203
193
  です。
194
+
195
+ ### 追記
196
+
197
+
198
+
199
+ 遷移後の画面ではこのように書いています。
200
+
201
+ ```swift
202
+
203
+ import UIKit
204
+
205
+
206
+
207
+ class NextViewController: UIViewController {
208
+
209
+
210
+
211
+  let speechWord: String? = recognizedWord
212
+
213
+   var speechWords: (String, String, String)?
214
+
215
+
216
+
217
+ override func viewDidLoad() {
218
+
219
+ super.viewDidLoad()
220
+
221
+ self.view.window?.backgroundColor = UIColor.clear
222
+
223
+ self.view.backgroundColor = UIColor.rgba(red: 0, green: 0, blue: 0, alpha: 0.5)
224
+
225
+ print(recognizedWord!)
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ ```
234
+
235
+
236
+
237
+ 遷移元の画面は、その前の画面に配置されたボタンを押すことで呼び出しています。
238
+
239
+ 上に複数のボタンが並び、それが押されるたびに画面がdismissして即座に別のViewControllerが呼び出されるようになっており、少し複雑な処理になっています。
240
+
241
+ 見返して考えましたが、もしかしたらここに原因?があるのかもしれないと思えてきました。
242
+
243
+
244
+
245
+ コードをくれた先輩と相談してみます。
246
+
247
+
248
+
249
+
250
+
251
+ ### 試したこと
252
+
253
+
254
+
255
+ self.view.window?.backgroundColor = UIColor.clear
256
+
257
+ を消しても、タイトルのようなエラーはやはり出てきて、遷移元の画面は消えてしまいました。