我正在通过在其中集成IBM Speech To Text服务来构建一个android应用程序,首先我录制音频并将其保存到设备上,然后进行转换,但在传递音频时,我收到了一个错误:";无法对数据流audio/wav进行转码->音频/x-foat-array";我也试着给出不同格式的音频,但每种格式都会出现相同的错误。虽然音频得到妥善保存,可以使用音乐播放器收听。所以,请帮我摆脱这个错误。
这里首先我使用MediaRecorder录制音频,然后将其保存到设备,然后发送给对话,但出现错误,我尝试使用所有可能的音频格式
fileName = getExternalCacheDir().getAbsolutePath() + "/" + "examples.wav";
try {
RecognizeOptions recognizeOptions = new RecognizeOptions.Builder()
.audio(new FileInputStream(fileName))
.contentType("audio/wav")
.model("en-US_BroadbandModel")
.build();
BaseRecognizeCallback baseRecognizeCallback =
new BaseRecognizeCallback() {
@Override
public void onTranscription
(SpeechRecognitionResults speechRecognitionResults) {
System.out.println(speechRecognitionResults);
}
@Override
public void onConnected() {
}
@Override
public void onError(Exception e) {
Log.i("Error", e.getMessage());
enableMicButton();
}
@Override
public void onDisconnected() {
enableMicButton();
}
@Override
public void onInactivityTimeout(RuntimeException runtimeException) {
}
@Override
public void onListening() {
}
@Override
public void onTranscriptionComplete() {
}
};
speechToText.recognizeUsingWebSocket(recognizeOptions,
baseRecognizeCallback);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
如果您使用的是watson开发人员云Java SDK,下面是一个传递正确HTTPMediaType
的示例
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options =
new RecognizeOptions.Builder()
.audio(audio)
.interimResults(true)
.contentType(HttpMediaType.AUDIO_WAV)
.build();
service.recognizeUsingWebSocket(
options,
new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
@Override
public void onDisconnected() {
lock.countDown();
}
});
你可以在这里找到完整的例子