(function($, doc) {
$.plusReady(function() {
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 rate = "16000";
var channel = "1";
// var cuid = 'plus.device.imei'; //mac和imei码data:audio/amr;base64,
var cuid = '';
var speech = '';
var token_url = "https://aip.baidubce.com/oauth/2.0/token?";
var len = '';
var grant_type = "grant_type=自己的&";
var APIkey = "client_id=自己的&";
var SecretKey = "client_secret=自己的&";
var AppID = "自己的";
var access_token = "";
var fileArr = [];
var ui = {
boxMsgSound: doc.querySelector('#voice'),
areaMsgList: doc.querySelector('#search'),
boxSoundAlert: doc.querySelector('#sound-alert'),
mistake: doc.querySelector('#mistake'),
voice: doc.querySelector('#voice'),
};
function msgTextFocus() {
ui.boxMsgText.focus();
setTimeout(function() {
ui.boxMsgText.focus();
}, 150);
}
//点击输入框话筒
ui.boxMsgSound.addEventListener('tap', function(event) {
recordCancel = false;
if(stopTimer) clearTimeout(stopTimer);
audio_tips.innerHTML = "点击完成";
ui.boxSoundAlert.classList.remove('rprogress-sigh');
setSoundAlertVisable(true);
jQuery('.shield').show();
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);
}, function(e) {
plus.nativeUI.toast("录音时出现异常: " + e.message);
});
}, false);
ui.boxSoundAlert.addEventListener('tap', function(event) {
setSoundAlertVisable(false);
jQuery('.shield').hide();
recorder.stop();
}, false);
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);
}
};
/**
* 录音语音文件转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(!pageUtils.netWork()) {
pageUtils.netToast();
} else {
voiceToWord(base64Str);
}
};
reader.readAsDataURL(file);
}, function(e) {
pageUtils.showToast("读写出现异常: " + e.message);
})
})
}
token();
//获取token,需要取百度语音自己获取,相关操作参考百度语音api
function token() {
if(mui.os.android) {
data2 = grant_type + APIkey + SecretKey;
} else if(mui.os.ios) {
APIkey = 'client_id=自己的&';
SecretKey = 'client_secret=自己的';
AppID = '自己的';
data2 = grant_type + APIkey + SecretKey;
}
mui.ajax(token_url, {
data: data2,
type: 'post',
contentType: "application/json; charset=utf-8",
timeout: 5000,
success: function(resp) {
access_token = resp.access_token;
},
error: function(xhr, type, errorThrown) {
pageUtils.showToast("网络请求出错");
}
});
}
//将语音转成文字
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(pageUtils.VOICEURL, {
data: data1,
type: 'post',
contentType: "application/json; charset=utf-8",
timeout: 5000,
success: function(resp) {
if(resp.result == undefined || resp.result == '') {
return;
}
jQuery('#mistake').show();
jQuery('#voice').hide();
ui.areaMsgList.value = resp.result[0];
},
error: function(xhr, type, errorThrown) {
if(type == 'timeout') {
pageUtils.showToast("录音超时");
} else {
pageUtils.showToast("网络请求出错");
}
}
});
}
//这里录音发送完成就不需要了,所以没有必要存在手机里,直接删除音频
function delVoiceFile() {
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();
// pageUtils.showToast('删除成功');
}, function(e) {
// pageUtils.showToast('删除失败');
});
}, 500);
}
}, function(e) {
alert("Resolve file URL failed: " + e.message);
});
}
}
});
}(mui, document));

绝地求生
- 发布:2018-06-06 17:41
- 更新:2019-07-24 10:07
- 阅读:3759
mui集成百度语音转写成文字
分类:MUI
7 个评论
要回复文章请先登录或注册
圣光神龙
绝地求生 (作者)
圣光神龙
6***@qq.com
1525941857QQ
Mike132
d***@sina.com