欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信网页录音功能

程序员文章站 2022-04-15 16:57:38
wx.ready(function () { var startRecordflag = false var startTime = null //btnRecord 为录音按钮dom对象 btnRecord.addEventListener('touchstart', function (even... ......
 wx.ready(function () {
        var startrecordflag = false
        var starttime = null
     //btnrecord 为录音按钮dom对象 btnrecord.addeventlistener('touchstart', function (event) { event.preventdefault(); starttime = newdate().gettime(); // 延时后录音,避免误操作 recordtimer = settimeout(function () { wx.startrecord({ success: function () { var rainallowrecord = sessionstorage.getitem("rainallowrecord");//判断是否授权过允许使用录音功能 if (!isempty(rainallowrecord) && rainallowrecord == "1") { //开始录音时的操作 如修改录音按钮样式等 } else { //一般第一次时 都没有授权 弹出授权窗口后 无法终止录音过程 所以在这里设置rainallowrecord 的值表示允许过录音 并且在第一次时主动停止录音 sessionstorage.setitem("rainallowrecord", "1"); wx.stoprecord(); } startrecordflag = true; }, cancel: function () { startrecordflag = true; alert('用户拒绝授权录音'); }, complete: function () { startrecordflag = true; } }); }, 300); }); btnrecord.addeventlistener('touchend', function (event) { event.preventdefault(); // 间隔太短 var timeditance = newdate().gettime() - starttime; if (timeditance < 300) { starttime = 0; // 不录音 cleartimeout(recordtimer); } else { // 松手结束录音 //startrecordflag 因为startrecord是个异步方法 防止没有进入startrecord 的回调就进入了这里 var startrecordhandle = setinterval(function () { //startrecordflag为true 表示已经进入过startrecord的回调 if (startrecordflag) { startrecordflag = false; clearinterval(startrecordhandle); wx.stoprecord({ success: function (res) { voice.localid = res.localid; translatevoice(); }, fail: function (res) { } }); } }, 0); } }); wx.onvoicerecordend({ complete: function (res) { voice.localid = res.localid; alert('录音时间已超过一分钟'); } }); function translatevoice() { //调用微信的语音转文字接口 wx.translatevoice({ localid: voice.localid, isshowprogresstips: 0, complete: function (res) { if (res.hasownproperty('translateresult')) { alert('识别结果:' + res.translateresult); } else { } } }); } function playvoice() { wx.playvoice({ localid: voice.localid }); } })