模拟微信按住说话功能

  • 微信调用stopReord是异步操作,太快调用该接口,fail都不会执行,会导致录音无法结束

  • wx.startRecord 安卓端不会进入该方法回调,但是可以成功唤起录音,参数也无效,该问题微信一直没有修复

  • 开始说话

     /** 
         * 开始录音
        */
        startVoice = () => {
            wx.ready(() => {
                wx.stopRecord({
                    fail: function (res) {
                    },
                    success: function (res) {
                        var localId = res.localId;
                        console.log('----结束说话成功--', localId)
                    }
                });
                this.recordTimeout = setTimeout(() => {
                    this.isStart = true;
                    nowTime = new Date().getTime();
                    console.log('-----开始录音---')
                    wx.startRecord({
                        maxDuration: 300,
                    });
                }, 500) // 最低按500ms
            })
        }

  • 松开按钮
    /** 
         * 结束录音
        */
        endVoice = () => {
            clearTimeout(this.recordTimeout)
            if (this.isStart) {
                setTimeout(() => {
                    wx.stopRecord({
                        success: (res) => {
                            lastTime = new Date().getTime();
                            let diff = lastTime - nowTime;
                            let seconds = diff % (60 * 1000) / 1000
                            console.log('-=-=-=-=-=-', this.isStart, seconds)
                            console.log('----结束说话成功--')
                            this.refs.recordVoice.className = 'action-record';
                            this.refs.inputVoice.className = '';
                            this.sendVioceMsg(res.localId);
                        },
                        fail: function (err) {
                            console.log('结束说话失败1')
                            // alert("结束说话失败");
                            Toast.fail('说话时间太短!')
                            setTimeout(() => {
                                wx.stopRecord({
                                    success: (res) => {
                                    },
                                    fail: function (err) {
                                        Toast.fail('结束说话失败')
                                        console.log('结束说话失败2')
                                    }
                                });
                            }, 500)
                        }
                    });
                    this.isStart = false;
                    startRecord = false;
                }, 500)
            } else {
                this.isStart = false;
                startRecord = false;
                Toast.fail('说话时间太短!')
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值