Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将麦克风中的数据音频转换为字节,以便在Google语音API上识别音频_Java_Audio_Google Speech Api - Fatal编程技术网

Java 将麦克风中的数据音频转换为字节,以便在Google语音API上识别音频

Java 将麦克风中的数据音频转换为字节,以便在Google语音API上识别音频,java,audio,google-speech-api,Java,Audio,Google Speech Api,在阅读了一些使用谷歌语音API的演示应用程序之后。他们使用文件音频进行演示 例如: SpeechClient speech = SpeechClient.create(); // The path to the audio file to transcribe String fileName = "./resources/RecordAudio.flac"; // Reads the audio file into memory Path path = Paths.get(fileName);

在阅读了一些使用谷歌语音API的演示应用程序之后。他们使用文件音频进行演示

例如:

SpeechClient speech = SpeechClient.create();
// The path to the audio file to transcribe
String fileName = "./resources/RecordAudio.flac";

// 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.FLAC)
 .setSampleRateHertz(16000)
 .setLanguageCode("vi-VI")
 .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();
记录之后,我得到了记录的数据,并保存在变量字节输出中

对于识别数据音频字节输出,我尝试将其写入RecordAudio.wav,并将RecordAudio.wav转换为RecordAudio.flac(我使用Audacity进行转换)。最后,使用Google语音API识别RecordAudio.flac文件。所以我意识到解决方案是缓慢而复杂的

所以,当阅读上面关于谷歌语音API的示例时,我认为与其在录制后直接将数据放在原始数据中,不如将其放在音频文件中进行识别。录制后的原始音频存储在字节输出中

但我真的不知道如何在上面的Google语音API示例中通过TestRing audioBytes将字节输出转换为

ByteString audioBytes = ByteString.copyFrom(data);

任何人都知道这个解决方案。非常感谢。

所以,你的问题归结为“如何通过TearrayOutputStream从
提取
字节[]
”,对吗?是的,它适合Google语音API的字节顺序。我希望AudioSystem会产生通过Google的
AudioEncoding.LINEAR16可识别的字节。而
ByteArrayOutputStream
上有
byte[]toByteArray()
。虽然可能最好改为流识别,以避免在内存中收集所有字节(AudioInputStream等)。非常感谢。我明白了,你的样品成功了吗?你能分享一下你的现成解决方案吗?我找不到一个好方法,如何从麦克风实时录制并转换为文本。。也许你能帮我。谢谢!
ByteString audioBytes = ByteString.copyFrom(data);