微信录音遇到的坑

模拟微信长按录音

第一次调用录音功能时,会弹框权限询问,所以要在长按之前先把alert干掉

wx.startRecord(); // 防止第一次弹框 TODO
                setTimeout(function(){
                    wx.stopRecord({
                        fail:function(res){
                            //alert("停止失败");
                        },
                        success: function (res) {
                            var localId = res.localId;
                        }
                    });
                },800); // 这里的 800 是血的教训,开始为了快写了10,
//坑爹的发现 ios会失败,而安卓正常,最后发现是stop本身异步,太快调用fail都不会执行,坑点之一
var left,top,width,height;
    // 长按说口令
    $("#start_voice_wrap").on("touchstart",function(e){
        var $this = $(this);
        left = $this.offset().left;
        top = $this.offset().top;
        width = $this.width();
        height = $this.height();
            // 开始录音
          // 初始化判断的值,微信初始化是否成功
            if(!newVersionInitFinish){
                alert('录音环境正在初始化中,请再试一次');
                return;
            }
        // 这个可以用来记录是否超过一秒从而生成一个判断条件,目前没用
            nowTime = new Date().getTime();
            startRecord=true; // 开始录音
            // 这个stopRecord 不能太快,最少超过500ms,再调用比较好,否则,不会有任何事情发生
        // 想拦个错误都坑爹
            wx.stopRecord({
                fail: function(res){
                },
                success: function (res) {
                    var localId = res.localId;
                }
            });
              // 超过500ms,才开始调用录音接口
            recordTimeout = setTimeout(function(){ // 成功停止后,开始第二轮录音
                if(startRecord){
                    isPlay = true; // 开始真正录音
                    startReady = true;
                    wx.stopRecord({
                        success: function (res) {
                            var localId = res.localId;
                        }
                    });
                    $("#chat_wrap").hide();
                    $("#record_count_down").show();
                    clearInterval(voiceId); // 清除音量动画
                    clearInterval(countId); // 清除倒计时动画
                    voiceId =  voice(voiceId); // 记录音量动画ID
                    countId =  countDown(countId); // 记录倒计时动画ID
                    //开始录音
                    wx.startRecord();
                }
            },500); // 最低按500ms
        e.preventDefault();
    }).on("touchmove",function(e){
        var touch=e.originalEvent.touches[0];
        var pageX=touch.pageX;
        var pageY=touch.pageY;
        lastTime = new Date().getTime();
        // 可以允许在上面滑动,当超出范围调用停止接口
        if(isPlay){
            if(pageX(left+width) || pageY(top+height)){// left top width height 左边右边
                clearInterval(voiceId); // 清除音量动画
                clearInterval(countId); // 清除倒计时动画
                $("#record_count_down").hide();
                $("#chat_wrap").show();
                // 结束录音
                if(startReady){
                    finishRecord(); // 触发事件
                }
                startReady=startRecord=false;
            }
        }
        e.preventDefault();
    }).on("touchend",function(e){
        lastTime = new Date().getTime();
        clearTimeout(recordTimeout); //清除初始按下的定时器
        if(isPlay){
            clearInterval(voiceId); // 清除音量动画
            clearInterval(countId); // 清除倒计时动画
            $("#record_count_down").hide();
            $("#chat_wrap").show();

            if(startReady){
                finishRecord(); 
            }
            startReady=startRecord=false;
            e.preventDefault();
        }
        e.preventDefault();
    });
wx.stopRecord({ // 停止成功后才会返回localId
            success: function (res) {
                var localId = res.localId;
                allData.localId = res.localId; // 存全局
                $("#voice_had").attr("data-id",res.localId); // 回填语音ID 
                wx.translateVoice({  // 转换语音接口
                    localId:localId, // 需要识别的音频的本地Id,由录音相关接口获得
                    isShowProgressTips: 0, // 默认为1,显示进度提示
                    fail:function(){
                        alert('没有检测到你的声音,大声一点,再试一次吧');
                        location.reload();
                    },
                    success: function (res2) {
                        allData.translateResult = res2.translateResult?res2.translateResult:'-'; // 存转换的文本
                        $("#voice_back_text").html(allData.translateResult); // 回填文本
                        wx.uploadVoice({
                            localId:localId, // 需要上传的音频的本地ID,由stopRecord接口获得
                            isShowProgressTips: 0, // 默认为1,显示进度提示
                                success: function (res3) {
                          })
              })

    // 点击播放
    $("#voice_had").on("touchend",function(){
        $(".voice-volume1").addClass("moveleft");
        var id = $(this).attr("data-id");
        var url = $(this).attr("data-audioUrl");
        var currAudio = $("#curr_audio")[0];
        var $this = $(this);
        if($("#list_items").find(".voice-wrap").length > 0){
            $("#list_items .voice-wrap").each(function(){
                $(this).find(".voice-wrap-inner1").removeClass("moveleft");
            });
        }
        if(id){
            wx.playVoice({localId: id});
            // 结束
            wx.onVoicePlayEnd({
                success: function (res) {
                    $(".voice-volume1").removeClass("moveleft");
                }
            });
        }else if(url){
            currAudio.src = url;
            currAudio.play();
            currAudio.addEventListener('ended',function(){
                $(".voice-volume1").removeClass("moveleft");
                stopVoice($this,id);
            });
        }
    });

你可能感兴趣的:(微信录音遇到的坑)