安装插件
代码语言:javascript复制cordova plugin add cordova-plugin-media-capture
移动端方法调取手机录音
代码语言:javascript复制audioCapture(){
navigator.device.audiorecorder.recordAudio(this.successCallback, this.errorCallback, 60);
},
成功回调方法:
代码语言:javascript复制successCallback(data){
this.filename = JSON.parse(data).file_name;
this.upload4audio(JSON.parse(data).full_path);
},
拿到录音在本地的保存文件直接上传至服务器
代码语言:javascript复制//使用FileTransfer插件,上传文件------语音文件
upload4audio(fileURL) {
var _this = this;
//上传成功
var success = function (r) {
var strs = JSON.parse(r.response);
_this.audiopath = JSON.parse(r.response).data.audioShowUrl;
_this.content = _this.audiopath;
_this.msgtype = 2;
_this.action_type = 'send_msg';
_this.send2Server();
_this.getHeight();
_this.message = '';
}
//上传失败
var fail = function (error) {
var str = JSON.stringify(error);
alert("转码失败请重试!" str)
}
var options = new FileUploadOptions();
options.fileKey = "file1";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') 1);
//上传参数
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
//上传地址
var SERVER = "http://81.68.107.23/api/upload/upload4audio"
ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
},