Я пытаюсь записать звук и речь в текст одновременно, но это не работает на всех Android устройствах. Аудиорекордер работает отлично, но на некоторых устройствах распознавание речи дает ошибку (КОД ОШИБКИ 2 и 5).
Ниже мой код:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private fun recordingStart() {
startRecordingAudio(activity)
recognizerIntent =
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) recognizerIntent !!. putExtra (RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en")
recognizerIntent!!.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity?.packageName)
recognizerIntent!!.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH
)
recognizerIntent!!.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity?.packageName)
recognizerIntent!!.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3)
speech!!.startListening(recognizerIntent)
}
private fun recordingStop() {
speech!!.stopListening()
stopRecordingAudio(activity)
}
private var mRecorder: MediaRecorder? = null
fun startRecordingAudio(ctx: Context?) {
mRecorder = MediaRecorder()
mRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
mRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mFileName = ctx?.let {
getStorageDirectory(it, "demo/audio")?.absolutePath
} + "/" + Date().time + ".mp3"
mRecorder?.setOutputFile(mFileName)
mRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mRecorder?.prepare()
mRecorder?.start()
}
fun stopRecordingAudio(ctx: Context): Uri? {
mRecorder?.stop()
mRecorder?.reset()
mRecorder?.release()
return saveAudio(ctx)
}