Код: Выделить всё
package expo.modules.myvoicemodule
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.speech.RecognitionListener
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import android.util.Log
import android.Manifest
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MyVoiceModule : Module() {
private var speechRecognizer: SpeechRecognizer? = null
private var recognitionIntent: Intent? = null
override fun definition() = ModuleDefinition {
Name("MyVoiceModule")
Function("startListening") {
Log.d("MyVoiceModule", "Starting...")
val activity = appContext.currentActivity
?: throw Exception("No activity available")
val permission = Manifest.permission.RECORD_AUDIO
val currentActivity = appContext.currentActivity
Log.d("MyVoiceModule", "Activity...${activity}")
if (currentActivity != null && ContextCompat.checkSelfPermission(currentActivity, permission) != PackageManager.PERMISSION_GRANTED) {
// Request permission if not granted
ActivityCompat.requestPermissions(currentActivity, arrayOf(permission), 1)
Log.d("MyVoiceModule", "Permission not granted, requesting permission")
return@Function null
}
Handler(Looper.getMainLooper()).post {
Log.d("MyVoiceModule", "Test handler is working")
Log.d("MyVoiceModule", "ActivityProvider: ${appContext.currentActivity}")
val currentActivity = appContext.currentActivity
Log.d("MyVoiceModule", "currentActivtyi: ${currentActivity}")
if (currentActivity != null) {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(currentActivity)
speechRecognizer?.setRecognitionListener(recognitionListener)
Log.d("MyVoiceModule", "SpeechRecognizer: ${speechRecognizer}")
recognitionIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
}
Log.d("MyVoiceModule", "SpeechRecognizer will start listening now...")
speechRecognizer?.startListening(recognitionIntent)
Log.d("MyVoiceModule", "SpeechRecognizer is listening now...")
} else {
Log.d("current activity", "Activity is null!")
}
}
}
Function("stopListening") {
Log.d("MyVoiceModule", "Stopping...")
Handler(Looper.getMainLooper()).post {
appContext.activityProvider?.currentActivity?.let { _ ->
speechRecognizer?.stopListening()
speechRecognizer?.destroy()
speechRecognizer = null
}
}
}
Events("onResults")
Events("onError")
}
private val recognitionListener = object : RecognitionListener {
override fun onReadyForSpeech(params: Bundle) {
Log.d("recognition listener"," Ready for speech!")
}
override fun onBeginningOfSpeech() {}
override fun onRmsChanged(rmsdB: Float) {}
override fun onBufferReceived(buffer: ByteArray) {}
override fun onEndOfSpeech() {}
override fun onError(error: Int) {
Log.d("OnError", "Error...${error}")
sendEvent("onError", mapOf(
"errorCode" to error
))
}
override fun onResults(results: Bundle) {
val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
Log.d("Speech Recognizer Results", "${matches}")
matches?.let {
sendEvent("onResults", mapOf(
"results" to it
))
}
}
override fun onPartialResults(partialResults: Bundle) {}
override fun onEvent(eventType: Int, params: Bundle) {}
}
}
Как только я нажму кнопку «Начать прослушивание»:
Код: Выделить всё
11-25 18:23:13.462 27006 27068 D MyVoiceModule: Starting...
11-25 18:23:13.463 27006 27068 D MyVoiceModule: Activity...com.anonymous.myexpoapp.MainActivity@8c53839
11-25 18:23:13.467 27006 27006 D MyVoiceModule: Test handler is working
11-25 18:23:13.467 27006 27006 D MyVoiceModule: ActivityProvider: com.anonymous.myexpoapp.MainActivity@8c53839
11-25 18:23:13.467 27006 27006 D MyVoiceModule: currentActivtyi: com.anonymous.myexpoapp.MainActivity@8c53839
11-25 18:23:13.467 27006 27006 D MyVoiceModule: SpeechRecognizer: android.speech.SpeechRecognizerProxy@e915243
11-25 18:23:13.467 27006 27006 D MyVoiceModule: SpeechRecognizer will start listening now...
11-25 18:23:13.473 27006 27006 D MyVoiceModule: SpeechRecognizer is listening now...
Код: Выделить всё
11-25 18:23:17.998 27006 27068 D MyVoiceModule: Stopping...
Что здесь не так?
Подробнее здесь: https://stackoverflow.com/questions/792 ... n-expo-app