我正在试验谷歌云语音到文本服务。它工作正常,但我无法更新 google-cloud-speech 的版本。如果我使用更新的版本而不是
0.41.0-alpha
,它会在我尝试启动它时立即抛出此错误:
16681-16681/com.example.voice_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.voice_test, PID: 16681
java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/String;)V in class
Lio/grpc/internal/AbstractManagedChannelImplBuilder; or its super classes (declaration of
'io.grpc.internal.AbstractManagedChannelImplBuilder' appears in /data/app/com.example.voice_test-
dOPemX43m_vBjq_Tc8tk7w==/base.apk:classes3.dex)
如何解决这个错误?
语音识别的 Gradle 依赖项:
implementation 'io.grpc:grpc-okhttp:1.10.0'
implementation 'com.google.cloud:google-cloud-speech:0.41.0-alpha'
创建语音客户端:
private val mSpeechClient by lazy {
applicationContext.resources.openRawResource(R.raw.credential).use {
SpeechClient.create(
SpeechSettings.newBuilder()
.setCredentialsProvider { GoogleCredentials.fromStream(it) }
.build())
}
}
OnCreate 函数。语音记录器本身使用 AudioRecord 类,没什么特别的。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mTextView = findViewById(R.id.last_recognize_result)
mSpeakButton = findViewById(R.id.button)
// get permissions
ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_RECORD_AUDIO_PERMISSION)
val config = RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode(LANGCODE)
.build()
val audioRecorder = AudioRecorder()
val clicker: View.OnClickListener = View.OnClickListener {
val byteArrayAudio: ByteString = audioRecorder.record(5000)
val audio: RecognitionAudio = RecognitionAudio.newBuilder()
.setContent(byteArrayAudio).build()
val resultsList = mSpeechClient.recognize(config, audio).resultsList
for (result in resultsList) {
val resultText = result.alternativesList[0].transcript
mTextView.setText(resultText)
Log.d(TAG, "-- Transcription: %s".format(resultText))
}
}
mSpeakButton.setOnClickListener(clicker)
}
我遇到了同样的问题,然后我更新了版本 - 实现 'io.grpc:grpc-okhttp:1.10.0' 将此版本更新为实现“io.grpc:grpc-okhttp:1.54.1”,谷歌解决了这个问题.