实现语音和视频支持
发送语音实现思路
打开允许录音的权限
然后把获取的录音上传到服务器,返回个公网可以访问路径
一、 采集语音
navigator.mediaDevices.getUserMedia(
{audio: true, video: true}
).then(successfunc).catch(errfunc);
navigator.mediaDevices.getUserMedia(
{audio: true, video: false}
).then(function(stream) {
//请求成功
this.recorder = new MediaRecorder(stream);
this.recorder.start();
this.recorder.ondataavailable = (event) => {
uploadblob("attach/upload",event.data,".mp3",res=>{
//获取语音的长度
var duration = Math.ceil((new Date().getTime()-this.duration)/1000);
this.sendaudiomsg(res.data,duration);
})
stream.getTracks().forEach(function (track) {
track.stop();
});
this.showprocess = false
}
}.bind(this)).catch(function(err){
mui.toast(err.msg)
this.showprocess = false
}.bind(this));
二、 上传语音
function uploadblob(uri,blob,filetype,fn){
var xhr = new XMLHttpRequest();
xhr.open("POST","//"+location.host+"/"+uri, true);
// 添加http头,发送信息至服务器时内容编码类型
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
fn.call(this, JSON.parse(xhr.responseText));
}
};
var _data=[];
var formdata = new FormData();
formdata.append("filetype",filetype);
formdata.append("file",blob)
xhr.send(formdata);
}