我正在制作一个将音频转换为文本的应用程序。我尝试了谷歌的语音到文本 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/