本文主要是介绍微信公众号h5网页-调用录音评测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、全局引入jweixin-1.6.0.js微信能力插件;
二、初始化微信配置,开启音频相关能力;
initWxConfig() {let url = window.location.href.split('#')[0]let data = { url: url }this.$http.get(this.$api.getTicket, { params: data }).then((res) => {let data = res.data || {}if (data.flag) {this.$nextTick(() => {wx.config({debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。appId: data.appId,timestamp: data.timestamp,nonceStr: data.noncestr,signature: data.signature,jsApiList: ['startRecord', 'stopRecord', 'playVoice', 'stopVoice', 'uploadVoice', 'translateVoice']})})}})},
三、录音上传评测;
wx.startRecord();
let _this = thiswx.stopRecord({success: function (res) {_this.localId = res.localIdwx.uploadVoice({localId: res.localId, // 需要上传的音频的本地ID,由stopRecord接口获得isShowProgressTips: 1, // 默认为1,显示进度提示success: function (res) { _this.serverId = res.serverId // 返回音频的服务器端IDlet params = {questionId: _this.paper.id,mediaId: res.serverId,paperId: _this.paper.paperId}// loading_this.$http.post(_this.$api.audioCheck, params).then((res) => { _this.nextFlag = true //通过}).finally(() => {Toast.clear()})}})}})
四、应用置于后台,定时器、延时器由于浏览器性能机制有所沉睡;采用visibilitychange及时处理;
document.removeEventListener("visibilitychange", this.checkViChange);
checkViChange(){ if (document.hidden) { // 隐藏this.cacheHiddenTime = moment().unix();if(this.setInterval){clearInterval(this.setInterval); this.setInterval = null;};}else{let cur = moment().unix() - this.cacheHiddenTime;this.countdown = this.countdown>cur ? (this.countdown-cur) : 0; this.countDownUtil();}},
这篇关于微信公众号h5网页-调用录音评测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!