import
android.content.Intent
import
android.content.pm.PackageManager
import
android.net.Uri
import
android.os.Build
import
android.os.Bundle
import
android.provider.Settings
import
android.speech.RecognitionListener
import
android.speech.RecognizerIntent
import
android.speech.SpeechRecognizer
import
android.widget.ImageView
import
android.widget.TextView
import
android.widget.Toast
import
androidx.appcompat.app.AppCompatActivity
import
androidx.core.content.ContextCompat
import
java.util.*
class
MainActivity : AppCompatActivity() {
private
lateinit var micIV: ImageView
private
lateinit var outputTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
micIV = findViewById(R.id.mic_speak_iv)
outputTV = findViewById(R.id.speak_output_tv)
micIV.setOnClickListener {
checkAudioPermission()
micIV.setColorFilter(ContextCompat.getColor(
this
, R.color.mic_enabled_color))
startSpeechToText()
}
}
private
fun startSpeechToText() {
val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(
this
)
val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
speechRecognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
speechRecognizer.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(bundle: Bundle?) {}
override fun onBeginningOfSpeech() {}
override fun onRmsChanged(v: Float) {}
override fun onBufferReceived(bytes: ByteArray?) {}
override fun onEndOfSpeech() {
micIV.setColorFilter(ContextCompat.getColor(applicationContext, R.color.mic_disabled_color))
}
override fun onError(i: Int) {}
override fun onResults(bundle: Bundle) {
val result = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
if
(result !=
null
) {
outputTV.text = result[
0
]
}
}
override fun onPartialResults(bundle: Bundle) {}
override fun onEvent(i: Int, bundle: Bundle?) {}
})
speechRecognizer.startListening(speechRecognizerIntent)
}
private
fun checkAudioPermission() {
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if
(ContextCompat.checkSelfPermission(
this
,
"android.permission.RECORD_AUDIO"
) != PackageManager.PERMISSION_GRANTED) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse(
"package:com.programmingtech.offlinespeechtotext"
))
startActivity(intent)
Toast.makeText(
this
,
"Allow Microphone Permission"
, Toast.LENGTH_SHORT).show()
}
}
}
}