@@ -12,29 +12,23 @@ import android.speech.RecognitionListener
12
12
import android.speech.RecognizerIntent
13
13
import android.speech.SpeechRecognizer
14
14
import androidx.annotation.StringRes
15
- import com.blankj.utilcode.util.ToastUtils
16
15
import com.osfans.trime.R
17
16
import com.osfans.trime.data.opencc.OpenCCDictManager
18
17
import com.osfans.trime.data.theme.ThemeManager
18
+ import com.osfans.trime.util.toast
19
19
import timber.log.Timber
20
20
import java.util.Arrays
21
21
22
22
/* * [語音輸入][RecognitionListener] */
23
- class Speech (context : Context ) : RecognitionListener {
24
- private val speechRecognizer: SpeechRecognizer ?
25
- private val recognizerIntent: Intent
26
-
27
- init {
28
- speechRecognizer = SpeechRecognizer .createSpeechRecognizer(context)
29
- speechRecognizer.setRecognitionListener(this )
30
- recognizerIntent = Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH )
31
- recognizerIntent.putExtra(RecognizerIntent .EXTRA_MAX_RESULTS , 1 )
32
- // recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
33
- // recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
34
- // recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
35
- // RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
36
- // recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "開始語音");
37
- }
23
+ class Speech (private val context : Context ) : RecognitionListener {
24
+ private val speechRecognizer =
25
+ SpeechRecognizer .createSpeechRecognizer(context).apply {
26
+ setRecognitionListener(this @Speech)
27
+ }
28
+ private val recognizerIntent: Intent =
29
+ Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH ).apply {
30
+ putExtra(RecognizerIntent .EXTRA_MAX_RESULTS , 1 )
31
+ }
38
32
39
33
fun startListening () {
40
34
speechRecognizer?.startListening(recognizerIntent)
@@ -57,7 +51,7 @@ class Speech(context: Context) : RecognitionListener {
57
51
speechRecognizer.stopListening()
58
52
speechRecognizer.destroy()
59
53
}
60
- ToastUtils .showShort (getErrorText(errorCode))
54
+ context.toast (getErrorText(errorCode))
61
55
}
62
56
63
57
override fun onEvent (
@@ -73,7 +67,7 @@ class Speech(context: Context) : RecognitionListener {
73
67
74
68
override fun onReadyForSpeech (arg0 : Bundle ) {
75
69
Timber .i(" onReadyForSpeech" )
76
- ToastUtils .showShort (" 請開始說話:" )
70
+ context.toast (" 請開始說話:" )
77
71
}
78
72
79
73
override fun onResults (results : Bundle ) {
0 commit comments