gpt4 book ai didi

android - 如何解决语音转文字错误: “unable to transcode data stream audio/wav -> audio/x-float-array”

转载 作者:行者123 更新时间:2023-12-03 01:11:24 24 4
gpt4 key购买 nike

我正在通过将IBM Speech-To-Text服务集成到其中来构建一个android应用,在那里我首先录制音频并将其保存到设备中,然后传递进行转换,但是传递音频时,我收到“无法对数据进行转码的错误”流音频/ wav->音频/ x-float-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-developer-cloud 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();
}
});
您可以找到完整的示例 here

关于android - 如何解决语音转文字错误: “unable to transcode data stream audio/wav -> audio/x-float-array”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63604755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com