Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 66|回复: 0

如何连接使用Google Speech-to-Text API

[复制链接]

401

主题

568

帖子

3340

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3340
发表于 7 天前 | 显示全部楼层 |阅读模式
1. 添加依赖项


在 app/build.gradle 文件中添加Google Cloud Speech依赖:
  1. dependencies {
  2.     implementation 'com.google.cloud:google-cloud-speech:2.5.1'
  3. }
复制代码


2. 设置认证

Google Speech-to-Text需要服务账户认证:
  ·创建Google Cloud项目并启用Speech-to-Text API
  ·下载服务账户密钥文件(JSON格式)
  ·将密钥文件放在 app/src/main/res/raw/ 目录下


3. 修改发送音频数据的方法

替换当前的 sendWavFile 方法,使用Google API:
  1. private void sendToGoogleSpeechAPI(byte[] audioData) {
  2.     new Thread(() -> {
  3.         try {
  4.             // 初始化Google Speech客户端
  5.             SpeechSettings speechSettings = SpeechSettings.newBuilder()
  6.                 .setCredentialsProvider(FixedCredentialsProvider.create(
  7.                     ServiceAccountCredentials.fromStream(
  8.                         context.getResources().openRawResource(R.raw.your_service_key)
  9.                     )
  10.                 ))
  11.                 .build();
  12.             
  13.             SpeechClient speechClient = SpeechClient.create(speechSettings);
  14.             
  15.             // 配置识别参数
  16.             RecognitionConfig config = RecognitionConfig.newBuilder()
  17.                 .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
  18.                 .setSampleRateHertz(8000)
  19.                 .setLanguageCode("en-US")
  20.                 .build();
  21.                
  22.             // 构建请求
  23.             RecognitionAudio audio = RecognitionAudio.newBuilder()
  24.                 .setContent(ByteString.copyFrom(audioData))
  25.                 .build();
  26.                
  27.             RecognizeRequest request = RecognizeRequest.newBuilder()
  28.                 .setConfig(config)
  29.                 .setAudio(audio)
  30.                 .build();
  31.                
  32.             // 发送请求并获取结果
  33.             RecognizeResponse response = speechClient.recognize(request);
  34.             List<SpeechRecognitionResult> results = response.getResultsList();
  35.             
  36.             // 处理识别结果
  37.             if (!results.isEmpty()) {
  38.                 String transcript = results.get(0).getAlternativesList().get(0).getTranscript();
  39.                 Log.d("MyAudioRecord", "识别结果: " + transcript);
  40.             }
  41.             
  42.             speechClient.close();
  43.         } catch (Exception e) {
  44.             Log.e("MyAudioRecord", "Google Speech API错误: " + e.getMessage());
  45.         }
  46.     }).start();
  47. }
复制代码


4. 调用修改

在 stopRecord 方法中调用新的API方法:
  1. // 替换原来的 sendWavFile() 调用
  2. sendToGoogleSpeechAPI(completeAudioData);
复制代码


这样就可以将当前项目中的音频数据发送到Google Speech-to-Text API进行识别了。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-11-3 06:41 , Processed in 0.014389 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表