android - 如何从音频文件中提取文本

标签 android

我正在制作一个将音频转换为文本的应用程序。我尝试了谷歌的语音到文本 API,但当你按下一个图标时它会工作,它会同时识别语音,但我有一个音频文件,我想将其转换为文本。
我搜索了很多,但我只得到语音到文本。

最佳答案

您可以使用 Google 的 Cloud Speech API。

将此添加到您的 gradle 文件中:

compile 'com.google.cloud:google-cloud-speech:0.30.0-alpha'

并使用此代码:

    // Instantiates a client
    SpeechClient speech = SpeechClient.create();

    // The path to the audio file to transcribe
    String fileName = "./resources/audio.raw";

    // Reads the audio file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString audioBytes = ByteString.copyFrom(data);

    // Builds the sync recognize request
    RecognitionConfig config = RecognitionConfig.newBuilder()
        .setEncoding(AudioEncoding.LINEAR16)
        .setSampleRateHertz(16000)
        .setLanguageCode("en-US")
        .build();
    RecognitionAudio audio = RecognitionAudio.newBuilder()
        .setContent(audioBytes)
        .build();

    // Performs speech recognition on the audio file
    RecognizeResponse response = speech.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();

    for (SpeechRecognitionResult result: results) {
      // There can be several alternative transcripts for a given chunk of speech. Just use the
      // first (most likely) one here.
      SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
      System.out.printf("Transcription: %s%n", alternative.getTranscript());
    }
    speech.close();

更多信息,请引用此链接: https://cloud.google.com/speech/docs/reference/libraries#client-libraries-install-java

关于android - 如何从音频文件中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471159/

相关文章:

android - .android 文件夹中没有 debug.keystore

Android CheckedTextView - 动态设置复选标记

android - Proguard RoboGuice ViewListener 上的混淆错误

android - URL 连接 - 无连接

android - recyclerview 或 viewpager 上的 setRotationY(180) 在 Android 9(API 28)中创建滚动问题

android - 当我从我的 Activity 中调用 getSystemService() 时,正在运行什么代码?

java - 将android图库中的图像保存/加载为位图?

java - 如何使用 TextWatcher 检查特定字符串而不是单个字符?

Android ListView加载图片慢

android - 使用 Google Maps Android API v2 缩放时标记会移动