cordova-plugin-media实现移动端录音功能

小编:管理员 913阅读 2022.09.13

安装插件
cordova plugin add cordova-plugin-media-capture
复制移动端方法调取手机录音
audioCapture(){
		navigator.device.audiorecorder.recordAudio(this.successCallback, this.errorCallback, 60);
},
复制

成功回调方法:

successCallback(data){
			this.filename = JSON.parse(data).file_name;
			this.upload4audio(JSON.parse(data).full_path);
		},
复制拿到录音在本地的保存文件直接上传至服务器
//使用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);
		  },
复制
关联标签: