从客户端发送音频到服务器

时间:2021-03-02 11:31:04

标签: javascript java audio

我正在尝试将音频从客户端 (javascript) 发送到服务器 (java)。我从麦克风中获取用户音频,然后从中制作一个 blob(以及 blob 的 url)。该项目是一个 Spring Boot 项目,所以我正在寻找一种方法将其作为参数发送到上传到服务器的方法中。

希望可以将 blob 上传到服务器,但它似乎只在浏览器本地可用,并且由于 blob 的 url 在“http”之前以“blob:”开头,因此会导致问题。

我也看过序列化,但似乎没有找到一种方法来用 js 中的 blob 来做到这一点。

只需在客户端和服务器之间传递 blob url

js 客户端

// Convert the audio data in to blob
                // after stopping the recording
                mediaRecorder.onstop = function (ev) {
                    console.log(dataArray);
                    // blob of type mp3
                    let audioData = new Blob(dataArray,
                        { 'type': 'audio/mp3;' });

                    // After fill up the chunk
                    // array make it empty
                    dataArray = [];

                    // Creating audio url with reference
                    // of created blob named 'audioData'
                    let audioSrc = window.URL
                        .createObjectURL(audioData);
                    //console.log(audioSrc);
                    // Pass the audio url to the 2nd video tag
                    playAudio.src = audioSrc;


                    const url = "http://localhost:8080/speech?url=" + audioSrc;
                    console.log(url);
                    $.get(url, function(data) {
                        $("#resultat").html("transcribed tekst: " + data);
                    });



                }

Java 服务器

@GetMapping("/speech")
    public String speechToText(String url) throws IOException {

        try (SpeechClient speechClient = SpeechClient.create()) {
            
            // The path to the audio file to transcribe
            String gcsUri = url;

            // Builds the sync recognize request
            RecognitionConfig config =
                    RecognitionConfig.newBuilder()
                            .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                            .setSampleRateHertz(16000)
                            .setLanguageCode("en-US")
                            .build();
            RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();

            // Performs speech recognition on the audio file
            RecognizeResponse response = speechClient.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());
                return alternative.getTranscript();
            }
            return "idk";
        } catch (IOException e) {
            e.printStackTrace();
            return "noe ble feil";
        }
    }

0 个答案:

没有答案