(function($, wvs) { var mwvs = function(uii, cback) { var dst; var src; var recordCancel = false; var recorder = null; var audio_tips = document.getElementById("audio_tips"); var startTimestamp = null; var stopTimestamp = null; var stopTimer = null; var path1 = null; if(mui.os.android) { var format = "amr"; } else if(mui.os.ios) { var format = "wav"; } var MIN_SOUND_TIME = 800; //录音使用变量 var rate = "16000"; var channel = "1"; var cuid = ''; var speech = ''; var token_url = "https://aip.baidubce.com/oauth/2.0/token?"; var len = ''; var grant_type = "client_credentials"; var APIkey = "jDBhIehFi6fAISLx01y2oeTY"; var SecretKey = "BtA42GbxGr6vfGppc8spPc5k62bz2keT"; var AppID = "14292369"; var access_token = ""; var fileArr = []; if(!uii) return; var ui = uii; function msgTextFocus() { ui.boxMsgText.focus(); setTimeout(function() { ui.boxMsgText.focus(); }, 150); } var setSoundAlertVisable = function(show) { if(show) { ui.boxSoundAlert.style.display = 'block'; ui.boxSoundAlert.style.opacity = 1; } else { ui.boxSoundAlert.style.opacity = 0; //fadeOut 完成再真正隐藏 setTimeout(function() { ui.boxSoundAlert.style.display = 'none'; }, 200); } }; ui.boxMsgSound.addEventListener('hold', function(event) { if(!ui.boxMsgSound.classList.contains('holdonmsg')) ui.boxMsgSound.classList.add('holdonmsg'); recordCancel = false; if(stopTimer) clearTimeout(stopTimer); audio_tips.innerHTML = "倾听中..."; ui.boxSoundAlert.classList.remove('rprogress-sigh'); setSoundAlertVisable(true); recorder = plus.audio.getRecorder(); if(recorder == null) { plus.nativeUI.toast("不能获取录音对象"); return; } startTimestamp = (new Date()).getTime(); recorder.record({ format: 'wav', samplerate: '16000', filename: "_doc/audio/" }, function(path) { fileArr.push(path); //录音完成 进入队列 方便删除本地录音。 if(recordCancel) return; Audio2dataURL(path); //语音转base64 发送百度 }, function(e) { plus.nativeUI.toast("录音时出现异常: " + e.message); }); }, false); ui.body.addEventListener('drag', function(event) { console.log('drag'); if(Math.abs(event.detail.deltaY) > 50) { if(!recordCancel) { recordCancel = true; if(!audio_tips.classList.contains("cancel")) { audio_tips.classList.add("cancel"); } audio_tips.innerHTML = "松开手指,取消发送"; } } else { if(recordCancel) { recordCancel = false; if(audio_tips.classList.contains("cancel")) { audio_tips.classList.remove("cancel"); } audio_tips.innerHTML = "手指上划,取消发送"; } } }, false); ui.boxMsgSound.addEventListener('release', function(event) { if(ui.boxMsgSound.classList.contains('holdonmsg')) ui.boxMsgSound.classList.remove('holdonmsg'); if(audio_tips.classList.contains("cancel")) { audio_tips.classList.remove("cancel"); audio_tips.innerHTML = "倾听中...请继续说..."; } // stopTimestamp = (new Date()).getTime(); if(stopTimestamp - startTimestamp < MIN_SOUND_TIME) { audio_tips.innerHTML = "录音时间太短"; ui.boxSoundAlert.classList.add('rprogress-sigh'); recordCancel = true; stopTimer = setTimeout(function() { setSoundAlertVisable(false); }, 800); } else { audio_tips.innerHTML = "识别中..."; // setSoundAlertVisable(false); } recorder.stop(); }, false); ui.boxMsgSound.addEventListener("touchstart", function(e) { e.preventDefault(); }); ui.photoimg.addEventListener('tap', function(event) { var btnArray = [{ title: "拍照" }, { title: "从相册选择" }]; plus.nativeUI.actionSheet({ title: "选择照片来源", cancel: "取消", buttons: btnArray }, function(e) { var index = e.index; switch(index) { case 0: break; case 1: var cmr = plus.camera.getCamera(); cmr.captureImage(function(path) { files = []; files.push(path) uploadd(files); }, function(err) { }); break; case 2: plus.gallery.pick(function(path) { files = []; files = path.files; uploadd(files); }, function(e) { }, { filter: "image", multiple: true, system: false, maximum: 10, onmaxed: function() { plus.nativeUI.alert('最多只能选择' + 10 + '张图片'); } }); break; } }); }, false); ui.boxMsgText.addEventListener('input', function() { var msg = ui.boxMsgText.innerHTML.replace(/(^\s*)|(\s*$)/g, ""); console.log(msg); if(msg) { ui.speetext.style.display = 'none'; ui.sendmsgg.style.display = ''; } else { ui.sendmsgg.style.display = 'none'; ui.speetext.style.display = 'block'; } }) /** * 录音语音文件转base64字符串 * @param {Object} path */ function Audio2dataURL(path) { plus.io.resolveLocalFileSystemURL(path, function(entry) { entry.file(function(file) { var reader = new plus.io.FileReader(); reader.onloadend = function(e) { var strResult = e.target.result; var index = strResult.indexOf('base64,') + 7; var base64Str = strResult.slice(index, strResult.length); delVoiceFile(); //删除本地语音 len = file.size; if(!app.isNetWork()) { alert("网络异常"); } else { voiceToWord(base64Str); } }; reader.readAsDataURL(file); }, function(e) { alert("读写出现异常: " + e.message); }) }) } token(); function token() { if(access_token) return; mui.ajax('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + APIkey + '&client_secret=' + SecretKey, { //data: data2, type: 'post', contentType: "application/json; charset=utf-8", timeout: 5000, success: function(resp) { //console.log(JSON.stringify(resp)); access_token = resp.access_token; }, error: function(xhr, type, errorThrown) { console.log(type + "网络请求出错"); } }); } //将语音转成文字 function voiceToWord(speech) { var data1 = { "rate": rate, "format": format, "channel": channel, "cuid": AppID, "token": access_token, "speech": speech, "dev_pid": 1536, "len": len }; mui.ajax('http://vop.baidu.com/server_api', { data: data1, type: 'post', contentType: "application/json; charset=utf-8", timeout: 5000, success: function(resp) { // alert(JSON.stringify(resp)); if(resp.result == undefined || resp.result == '') { audio_tips.innerHTML = "没听清,再说一遍"; stopTimer = setTimeout(function() { setSoundAlertVisable(false); }, 800); return; } if(ui.speetext.children[0].classList.contains('mui-icon-compose')) { ui.boxMsgText.style.display = 'block'; ui.boxMsgSound.style.display = 'none'; ui.speetext.children[0].classList.remove('mui-icon-compose'); } setSoundAlertVisable(false); ui.speetext.style.display = 'none'; ui.sendmsgg.style.display = ''; ui.boxMsgText.innerHTML = resp.result[0]; }, error: function(xhr, type, errorThrown) { if(type == 'timeout') { pageUtils.showToast("录音超时"); } else { pageUtils.showToast("网络请求出错"); } } }); } //这里录音发送完成就不需要了,所以没有必要存在手机里,直接删除音频 function delVoiceFile() { //return; //先测试不删除 for(var i = 0; i < fileArr.length; i++) { plus.io.resolveLocalFileSystemURL(fileArr[i], function(entry) { if(entry.isFile) { setTimeout(function() { entry.remove(function(entry) { fileArr.pop(); //alert('删除成功'); }, function(e) { alert('语音缓存删除失败' + e.message); }); }, 500); } }, function(e) { alert("Resolve file URL failed: " + e.message); }); } } //讨论 上传图片 function uploadd(files) { for(var i = 0; i < files.length; i++) { file = files[i]; var b = (i + 1 == files.length) ? true : false; audio_tips.innerHTML = "发送中..."; setSoundAlertVisable(true); var server = interaddress + 'simditor/upload'; var utask = plus.uploader.createUpload(server, //Indexx { method: "POST" }, function(t, status) { console.log(b) if(b) setSoundAlertVisable(false); //上传完成 if(status == 200) { console.log(t.responseText); var ret = JSON.parse(t.responseText); if(ret.success == true) { // var imgurl = ret.file_path.replace(ret.file_path.match(new RegExp('/(\\w+).?'))[0], interaddress); var img = ''; cback({ img: img, isall: b }); } else { mui.alert(ret.msg); } } else { if(utask) { utask.abort(); utask = null; } } }); utask.addEventListener("statechanged", function(tas, status) { if(!utask) { return; } var time = new Date(); switch(tas.state) { case 3: // 已接收到数据 var B = (parseInt(tas.uploadedSize) / parseInt(tas.totalSize)).toFixed(2) * 100; audio_tips.innerHTML = "发送中..." + B; break; } }); utask.addFile(file, { key: 'upfile' }); utask.start(); } } } window.voicespeech = mwvs; })(mui, window.voicespeech = {})