首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌SpeechClient API

谷歌SpeechClient API
EN

Stack Overflow用户
提问于 2018-11-01 13:12:17
回答 2查看 1.1K关注 0票数 0

我正在尝试使用做一些工作。如何使用Google库指定身份验证密钥?我正在使用这个方法Google语音API凭证

我想把音频文件转换成文本

谢谢

代码语言:javascript
复制
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());
            }
        }

build.gradle

代码语言:javascript
复制
dependencies {
   implementation fileTree(include: ['*.jar'], dir: 'libs')
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   implementation 'com.android.support:support-v4:28.0.0'
   implementation 'com.android.support:design:28.0.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:6.6.1') {
       transitive = true
   }
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.10.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-localization:0.5.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-core:0.2.1'
   //firebase
   implementation 'com.google.firebase:firebase-core:16.0.4'
   implementation 'com.google.firebase:firebase-auth:16.0.4'
   implementation 'com.google.firebase:firebase-database:16.0.4'
   implementation 'com.writingminds:FFmpegAndroid:0.3.2'
   implementation 'com.karumi:dexter:4.2.0' // this is use for runtime permissions
   implementation 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   annotationProcessor 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   implementation 'com.android.support:multidex:1.0.3'
 }
EN

回答 2

Stack Overflow用户

发布于 2018-11-06 04:51:20

在使用了凭据的JSON文件之后,将其放在Android项目源代码中的app/src/main/res/ 已生成 /credential.json下面。然后在语音服务中创建代码:

代码语言:javascript
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2018-11-05 19:47:50

我对Google没有多少经验,但据我所知,您应该:

在下创建项目。

在创建项目并设置付款方法(因为此服务不是免费的)之后,您将能够以JSON文件的形式下载私钥。

然后,您需要在机器上设置一个环境变量名GOOGLE_APPLICATION_CREDENTIALS,以指向该文件。那你应该准备好走了。

您可以一步一步地阅读如何这里的详细内容。

点击蓝色按钮,然后继续.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53102015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档