问题描述
我需要将我使用著名的 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 (将#修改为@)