微信语音(录音,上传,下载,播放,停止)

1.api:微信:JSSDK    https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

(1)初始化微信:一般在socket初始化后

initWx() {
            let _this = this;
            wx.config(_this.wxConfig);
            //检测环境是否能使用下面方法
            wx.checkJsApi({
                jsApiList: [
                'startRecord',
                'onVoiceRecordEnd',
                'stopRecord',
                'playVoice',
                'pauseVoice',
                'stopVoice',
                'onVoicePlayEnd',
                'uploadVoice',
                'downloadVoice'
                ],
                success:function(res) {
                    console.log(222222222);
                    console.log(res);
                    alert(JSON.stringify(res));
                }
            });       
            wx.ready(function () {
                //微信初始化完成时检测是否同意启用录音功能
                wx.startRecord({
                    success:function(res) {
                        wx.stopRecord({
                            success: function (res) {
                                // var localId = res.localId;
                            }
                        });
                    }
                });
            });
        },

(1)录音 

微信语音(录音,上传,下载,播放,停止)_第1张图片

长按说话
{ {voiceTimeing}}"
{ {voice_toast_text}}
onTouchstart:function(e){
            e.preventDefault();
            var _this = this;
            _this.voiceTimeing = 0;
            if(_this.recording == false) {
                _this.pageY = e.touches[0].pageY;
                _this.startTime = new Date().getTime();
                _this.timeOutVoice = setTimeout(function(){
                    _this.longClickVoice = true;
                    // _this.$fui.toast("长按");
                    if(_this.voiceTimeingOut) {
                        clearInterval(_this.voiceTimeingOut);
                        _this.voiceTimeingOut = null;
                    }
                    _this.voiceTimeing = 0;
                    //开始录音
                    wx.startRecord({
                        success:function() {
                            // _this.$fui.toast("录音开始");
                            _this.recording = true;
                            _this.voiceTimeingOut = setInterval(function() {
                                //环形进度条
                                _this.voiceTimeing ++;
                                _this.voiceCircleDeg =(18/2.5) * _this.voiceTimeing;
                                console.log(_this.voiceCircleDeg);
                                var semicircle1 = _this.$refs.semicircle1;
                                var semicircle2 = _this.$refs.semicircle2;
                                var semicircle3 = _this.$refs.semicircle3;
                                if(_this.voiceCircleDeg < 180) {
                                    semicircle2.style.transform = "rotate(" + _this.voiceCircleDeg + "deg)";
                                } else {
                                    semicircle1.style.transform = "rotate(" + (_this.voiceCircleDeg-180) + "deg)";
                                    semicircle2.style.display = "none";
                                    semicircle3.style.display = "inline-block";
                                }
                                //录音最长时间为50秒
                                if(_this.voiceTimeing > 50) {
                                    if(_this.voiceTimeingOut) {
                                        clearInterval(_this.voiceTimeingOut);
                                        _this.voiceTimeingOut = null;
                                    }
                                    _this.voiceEnd(e);
                                    // _this.$fui.toast("发送成功");
                                } 
                            },1000);
                            _this.voice_toast_text = "上滑取消";
                        },
                        cancel:function() {
                            _this.$fui.toast("拒绝录音");
                        }
                    })
                },500);
                console.log(_this.timeOutVoice);
            }
        },
        onTouchmove:function(e) {
            var _this = this;
            if(_this.recording == true && _this.longClickVoice) {
                e.preventDefault();
                var currentPageY = e.changedTouches[0].pageY;
                console.log(currentPageY - _this.pageY);
                //上移预设取消录音
                if(currentPageY - _this.pageY < -100) {
                    _this.cancelVoice = true; 
                    // _this.$fui.toast("显示如果松开,取消录音");
                    _this.voice_toast_text = "松手取消";
                } else {
                    // _this.$fui.toast("界面显示在录音");
                    _this.cancelVoice = false; 
                    _this.voice_toast_text = "上滑取消";

                }
            }
        },
        onTouchend:function(e) {
            var _this = this;
            if(_this.voiceTimeing > 50) {
                _this.voiceTimeing = 0;
                return false;
            } else {
                if(_this.voiceTimeingOut) {
                    clearInterval(_this.voiceTimeingOut);
                    _this.voiceTimeingOut = null;
                }
                _this.voiceTimeing = 0;
                _this.voiceEnd(e);
            }
        },
        //录音结束
        voiceEnd:function(e) {
            var _this = this;
            if(_this.longClickVoice){
                e.preventDefault();
                _this.endTime = new Date().getTime();
                _this.voiceTime = parseInt((_this.endTime - _this.startTime)/1000);
                //取消录音
                if(_this.cancelVoice == true) {
                    wx.stopRecord({
                        success: function (res) {
                            console.log(res);
                        }
                    });
                    // _this.$fui.toast("取消录音");
                    _this.cancelVoice = false;
                } else {
                    //录音太短
                    if(_this.voiceTime < 1) {
                        wx.stopRecord({
                            success: function (res) {
                                console.log(res);
                            }
                        });
                        _this.$fui.toast("录音太短");
                    } else {
                        //录音成功
                        wx.stopRecord({
                            success: function (res) {
                                console.log(res);
                                // _this.$fui.toast("录音成功");
                                let localId = res.localId;
                                //上传微信并下载到服务器
                                _this.getVoice(localId);
                            },
                            fail:function(res) {
                                console.log(res);
                                _this.$fui.toast("录音失败");

                            }
                        });
                    }
                    
                } 
            }
            _this.recording = false;
            if(_this.timeOutVoice) {
            clearTimeout(_this.timeOutVoice);
                _this.timeOutVoice = null;
            }
            _this.popup_status = 0;
        },
        //上传语音
        getVoice:function(localId) {
            var _this = this;
            wx.uploadVoice({
                localId: localId, // 需要上传的音频的本地ID,由stopRecord接口获得
                isShowProgressTips: 1, // 默认为1,显示进度提示
                success: function (res) {
                    // _this.$fui.toast("录音上传成功");
                    var serverId = res.serverId; // 返回音频的服务器端ID
                    sessionStorage.setItem(serverId,localId);
                    _this.pChatSocket.sendMessage(_this.voiceTime,"",serverId);
                },
                fail:function(res) {
                    console.log(res);
                    _this.$fui.toast("发送语音失败");
                }
            });
        },

(3)播放语音

onTap:function(msg_id,msg_rtype,msg_voice_url) {
            var _this = this;
            if(_this.playingMsgId != 0 && _this.playingMsgId == msg_id) {
                wx.pauseVoice({
                    localId: _this.voiceLocalId, // 需要停止的音频的本地ID,由stopRecord接口获得
                    success:function() {
                        _this.playingMsgId = 0;
                        _this.voiceLocalId = '';
                    }
                });
                wx.stopVoice({
                    localId: _this.voiceLocalId, // 需要停止的音频的本地ID,由stopRecord接口获得
                    success:function() {
                        _this.playingMsgId = 0;
                        _this.voiceLocalId = '';
                    }
                });
            } else {
                _this.playingMsgId = msg_id;
                if(msg_rtype == 2) {
                    console.log(msg_voice_url);
                    var localId = sessionStorage.getItem(msg_voice_url);
                    console.log("log_localId:" + localId);
                    if(!localId) {
                        console.log("播放下载音乐");
                        // //下载语音
                        wx.downloadVoice({
                            serverId: msg_voice_url, // 需要下载的音频的服务器端ID,由uploadVoice接口获得
                            isShowProgressTips: 1, // 默认为1,显示进度提示
                            success: function (res) {
                                var localId = res.localId; // 返回音频的本地ID
                                sessionStorage.setItem(msg_voice_url,localId);
                                _this.voiceLocalId = localId;
                                console.log("voiceLocalId:" + _this.voiceLocalId);
                                // _this.$fui.toast("录音下载成功");
                                //播放语音
                                wx.playVoice({
                                    localId: localId, // 需要播放的音频的本地ID,由stopRecord接口获得
                                    success:function(res) {
                                        console.log(res);
                                        // _this.$fui.toast("录音播放成功");
                                            wx.onVoicePlayEnd({
                                                // 录音时间超过一分钟没有停止的时候会执行 complete 回调
                                                complete: function (res) {
                                                    var localId = res.localId;
                                                    _this.playingMsgId = 0;
                                                    _this.voiceLocalId = '';
                                                }
                                            });
                                    },
                                    fail:function(res) {
                                        console.log(res);
                                        _this.$fui.toast("语音播放失败");
                                    }
                                });
                            },
                            fail:function(res) {
                                console.log(res);
                                _this.$fui.toast("语音下载失败");
                                // _this.downloadFail = true;
                                var self_msgList = _this.msgList;
                                for(var i = 0,l=self_msgList.length; i 

 

你可能感兴趣的:(js示例,vue)