Android Google SpeechClient API
我正在尝试使用谷歌语音API做一些工作。如何使用Google语音API Java库指定身份验证密钥? 我正在使用这种方法 我想把音频文件转换成文本 多谢各位Android Google SpeechClient API,android,speech-recognition,google-speech-api,Android,Speech Recognition,Google Speech Api,我正在尝试使用谷歌语音API做一些工作。如何使用Google语音API Java库指定身份验证密钥? 我正在使用这种方法 我想把音频文件转换成文本 多谢各位 CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("home/hussain/AndroidStudioProjects/Nest
CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("home/hussain/AndroidStudioProjects/NestedLogics/RouteApplication/service-account.json")));
SpeechSettings settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
//SpeechClient speechClient = SpeechClient.create(settings);
SpeechClient speech = SpeechClient.create(settings);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Path path = Paths.get(file_path);
byte[] data = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(data);
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode("en-US")
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
.build();
RecognizeResponse response = speech.recognize(config,audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result: results)
{
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
Log.d("HOME", "convertAudioToSpeech: "+alternative.getTranscript());
}
}
我对Google Cloud SDK没有太多经验,但据我所知,您应该: 在Google云服务下创建项目 创建项目并设置付款方式后,由于此服务不是免费的,您将能够以JSON文件的形式下载私钥 然后,您需要在您的计算机上设置一个环境变量名GOOGLE\u APPLICATION\u CREDENTIALS,以指向该文件。 那你应该准备好出发了 您可以一步一步地阅读详细的操作方法 单击蓝色按钮并从那里继续…在创建了一个带有凭据的JSON文件后,将其放在Android项目源中的app/src/main/res/raw/credential.JSON下。然后在语音服务中创建代码:
InputStream stream = getContext().getResources().openRawResource(R.raw.credential);
SpeechSettings settings =
SpeechSettings.newBuilder().setCredentialsProvider(
new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return GoogleCredentials.fromStream(stream);
}
}
).build();
你在使用Google Cloud SDK吗?是的@ItamarKerbeli尝试了这个,但是我得到了一个错误,原因是:java.lang.NoSuchMethodError:No direct method Lcom/Google/api/gax/rpc/ClientContext;V类Lcom/google/api/gax/rpc/ClientSettings$Builder;或者它的超级类声明'com.google.api.gax.rpc.ClientSettings$Builder'出现在/data/app/com.example.hussain.routeapplication-BtQ8fKEs7dp_ENQO4aatuw==/split_lib_dependencies_apk.apk,位于com.google.cloud.speech.SpeechSettings.v1.SpeechSettings$Builder.SpeechSettings.java:259,很难从这个错误消息中说出来。这很可能是依赖项的问题,因此请检查gradle配置是否与示例应用程序中的完全相同。或者可能是您正在使用过时的google cloud speech版本。我的是实现'com.google.cloud:googlecloudspeech:0.41.0-alpha'Alexander。。build.gradle在上面。@hussainabas我建议将依赖项版本从0.30.0更改为0.41.0-alpha
InputStream stream = getContext().getResources().openRawResource(R.raw.credential);
SpeechSettings settings =
SpeechSettings.newBuilder().setCredentialsProvider(
new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return GoogleCredentials.fromStream(stream);
}
}
).build();