微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JS base64 编码 wav 二进制文件

如何解决JS base64 编码 wav 二进制文件

我需要将我使用著名的 wav 二进制文件编码为 base64 https://github.com/mattdiamond/Recorderjs 项目:

function ($scope) {
    var ctrl = this;
    var gumStream;
    //stream from getUserMedia() 
    var rec;
    //Recorder.js object
    var input;

    var audioContext = new AudioContext();
    //new audio context to help us record 
    var recordButton = document.getElementById("recordButton");
    var stopButton = document.getElementById("stopButton");
    $scope.properties.value = undefined;
    var srConfig = {"config":
        {
            "sampleRateHertz":16000,"languageCode": $scope.properties.languageCode
        },"audio": 
            {"content":""}
            
    };
        /* disable the record button until we get a success or fail from getUserMedia() */


    this.startRecording = function(){
        console.log("recordButton clicked");

        var constraints = { audio: true,video:false };
        recordButton.disabled = true;
        stopButton.disabled = false;
        navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
            console.log("getUserMedia() success,stream created,initializing Recorder.js ...");
            document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz";
            /*  assign to gumStream for later use  */
            srConfig.config.sampleRateHertz = audioContext.sampleRate;
            gumStream = stream;
            /* use the stream */
            input = audioContext.createmediastreamsource(stream);
            rec = new Recorder(input,{numChannels:1});
            //start the recording process
            rec.record();

            console.log("Recording started");
            setTimeout(function(){
              ctrl.stopRecording();
            },parseInt($scope.properties.secondsToRegister)*1000);

        }).catch(function(err) {
            //enable the record button if getUserMedia() fails
            console.log("Error in getUserMedia");
            console.log(err);
            recordButton.disabled = false;
            stopButton.disabled = true;
        });
    };

    this.stopRecording =function(){
        console.log("stopButton clicked");
        //disable the stop button,enable the record too allow for new recordings 
        stopButton.disabled = true;
        recordButton.disabled = false;
        //tell the recorder to stop the recording 
        rec.stop(); //stop microphone access 
        gumStream.getAudioTracks()[0].stop();
        //create the wav blob and pass it on to createDownloadLink 
        var response = rec.exportWAV(this.callDetectorService);

    };
    
    this.callDetectorService = function(blob){
              var xhr=new XMLHttpRequest();
              if ("withCredentials" in xhr){
                  console.log("withCredentials supported");
              }
              else{
                  console.log("withCredentials NOT supported");
              }
              var response;
              xhr.onload=function(e) {
                  if(this.readyState === 4) {
                      console.log("Server returned: ",e.target.responseText);
                      $scope.$apply(function () {
                            $scope.properties.value = JSON.parse(e.target.responseText);
                      });
                      
                      response = e.target.responseText;
                  }
              };

              srConfig.audio.content=btoa(unescape(encodeURIComponent(blob)));
              console.log(srConfig);
              xhr.open("POST",$scope.properties.serviceUrl+"?key="+$scope.properties.secretkey,true);
              var data = JSON.stringify(srConfig);
              xhr.send(data);

    };
    

}

提交请求时(行 xhr.send(data)),我收到以下错误

服务器返回:{ “错误”: { “代码”:400, "message": "无效识别 'config': 错误编码..",“状态”:“INVALID_ARGUMENT” } }

这让我认为错误出在进行编码的行中:

srConfig.audio.content=btoa(unescape(encodeURIComponent(blob)));

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。