@@ -90,25 +90,6 @@ class MainActivity : AppCompatActivity() {
9090
9191 private fun onclick () {
9292 if (! isRecording) {
93- var keywords = inputText.text.toString()
94-
95- Log .i(TAG , keywords)
96- keywords = keywords.replace(" \n " , " /" )
97- keywords = keywords.trim()
98- // If keywords is an empty string, it just resets the decoding stream
99- // always returns true in this case.
100- // If keywords is not empty, it will create a new decoding stream with
101- // the given keywords appended to the default keywords.
102- // Return false if errors occurred when adding keywords, true otherwise.
103- stream.release()
104- stream = kws.createStream(keywords)
105- if (stream.ptr == 0L ) {
106- Log .i(TAG , " Failed to create stream with keywords: $keywords " )
107- Toast .makeText(this , " Failed to set keywords to $keywords ." , Toast .LENGTH_LONG )
108- .show()
109- return
110- }
111-
11293 val ret = initMicrophone()
11394 if (! ret) {
11495 Log .e(TAG , " Failed to initialize microphone" )
@@ -122,17 +103,41 @@ class MainActivity : AppCompatActivity() {
122103 lastText = " "
123104 idx = 0
124105
106+ var keywords = inputText.text.toString()
107+ Log .i(TAG , " Raw keywords: $keywords " )
108+
109+ keywords = keywords.replace(" \n " , " /" )
110+ keywords = keywords.trim()
111+
112+ Log .i(TAG , " Normalized keywords: $keywords " )
113+
114+ stream = kws.createStream(keywords)
115+ if (stream.ptr == 0L ) {
116+ Log .i(TAG , " Failed to create stream with keywords: $keywords " )
117+
118+ Toast .makeText(this , " Failed to set keywords to $keywords ." , Toast .LENGTH_LONG )
119+ .show()
120+
121+ audioRecord?.let {
122+ it.stop()
123+ it.release()
124+ }
125+ audioRecord = null
126+
127+ return
128+ }
129+
130+ Log .i(TAG , " Created stream. Running ..." )
131+
125132 recordingThread = thread(true ) {
126133 processSamples()
127134 }
135+
128136 Log .i(TAG , " Started recording" )
129137 } else {
130138 isRecording = false
131- audioRecord!! .stop()
132- audioRecord!! .release()
133- audioRecord = null
139+
134140 recordButton.setText(R .string.start)
135- stream.release()
136141 Log .i(TAG , " Stopped recording" )
137142 }
138143 }
@@ -175,6 +180,16 @@ class MainActivity : AppCompatActivity() {
175180 }
176181 }
177182 }
183+
184+ stream.release()
185+ Log .i(TAG , " Released stream. Stopped" )
186+
187+ audioRecord?.let {
188+ it.stop()
189+ it.release()
190+ }
191+
192+ audioRecord = null
178193 }
179194
180195 private fun initMicrophone (): Boolean {
@@ -217,6 +232,5 @@ class MainActivity : AppCompatActivity() {
217232 assetManager = application.assets,
218233 config = config,
219234 )
220- stream = kws.createStream()
221235 }
222- }
236+ }
0 commit comments