1

I am making an app that converts audio to text. I tried google's speech-to-text API, but it works when you press an icon it will recognize the voice same time, but I have an audio file which I want to convert to text.
I searched a lot, but I am getting only speech to text.

devgun
  • 1,003
  • 13
  • 33
androidking
  • 207
  • 5
  • 17

2 Answers2

1

You can using Google's Cloud Speech API.

Add this to your gradle file :

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

and use this code :

    // 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();

For more, refer this link : https://cloud.google.com/speech/docs/reference/libraries#client-libraries-install-java

Sahil Munjal
  • 463
  • 2
  • 15
0

May be you can find what you wanted from this stackoverflow question. There is a sample github project which you can refer to identify the workaround.

devgun
  • 1,003
  • 13
  • 33