如何解决语音转文字错误:“无法对数据流音频/音频->音频/ x浮动数组进行转码”

时间:2020-08-26 19:46:36

标签: android audio ibm-cloud speech-to-text

我通过在其中集成IBM Speech-To-Text服务来构建一个android应用程序,在那里我首先录制音频并将其保存到设备中,然后传递进行转换,但是传递音频时,我收到“无法进行错误提示”转码数据流音频/音频->音频/ 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();
                    }
               

1 个答案:

答案 0 :(得分:0)

如果您使用的是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