cocoscreator怎么使用微信jssdk的录音功能?

var localId;
cc.Class({
    extends: cc.Component,

    properties: {
        recordNode:cc.Node,
        timePro:cc.ProgressBar,
        timeLabel:cc.Label,
    },
    onLoad () {
        this.recordStart = false;
        this.timer = 10;
        this.recordNode.on("touchstart",this.startRecording,this);
        wx.ready(function () {
            wx.startRecord({
              cancel: function () {
                alert('用户拒绝授权录音');
              }
            });
            wx.stopRecord({
              success: function (res) {
                localId = res.localId;
                alert("localId"+localId);
              },
              fail: function (res) {
                alert(JSON.stringify(res));
              }
            });
            wx.playVoice({
                localId: localId, // 需要播放的音频的本地ID,由stopRecord接口获得
            });
        });
    },
    startRecording:function(){
        wx.startRecord({
          cancel: function () {
            alert('用户拒绝授权录音');
          }
        });
        this.recordNode.scaleX = 1.2;
        this.recordNode.scaleY = 1.2;
        this.recordStart = true;
        this.timer = 10;
        this.timeLabel.string = this.timer+"s";
        this.timePro.progress = 1;
        this.schedule(this.timecut,1);
        
    },
    timecut:function(){
        this.timer--;
        this.timeLabel.string = this.timer+"s";
        if(this.timer>=1){
        }
        else{
            this.stopRecord();
        }
        
    },
    obtainRecord:function(){
        var record = recorder.getBlob();  
        debugger;  
    },
    stopRecord:function(){
        this.recordNode.scaleX = 1;
        this.recordNode.scaleY = 1;
        this.recordStart = false;
        this.unschedule(this.timecut);
        wx.stopRecord({
          success: function (res) {
            localId = res.localId;
            alert("localId"+localId);
          },
          fail: function (res) {
            alert(JSON.stringify(res));
          }
        });
    },
    playRecord:function(){
        if (localId == '') {
          alert('请先录制一段声音');
          return;
        }
        wx.playVoice({
            localId: localId, // 需要播放的音频的本地ID,由stopRecord接口获得
        });
    },
    update (dt) {
        if(this.recordStart){
            this.timePro.progress-=dt/10;
        }
    },
});

这是我写的 但是在实际使用中 允许录音的alert 始终不出现 导致stopRecord 后 alert了 promission denied

1赞

为什么在wx.ready里要把所有的接口调用一遍呢?

乱写的
现在的问题是debug模式下 startRecord success了 但是不弹出录音授权的对话框 导致stopRecord 接口 fail playVoice接口fail

你怎么知道startRecord成功了呢?我看你并没有又startRecord的sucess注册过啊

另一个文件注入了 debug:true 调试的时候看到了 弹出了对话框 “errMsg”:“sign true”; 差不多这样的内容

而stopRecord 弹出的对话框是 “errMsg”:“promission denied”;
playVoice也是

你有做过微信的录音吗 有的话可以给个demo看看吗… 没有的话 你能先去试试吗… 说不定有同样的问题呢?

我很久以前做过,我只是想给你讨论一下思路,而且微信js sdk跟引擎关系应该不大,既然已经弹出permission denied了,那应该就是权限不够,刚查了接口权限,录音接口不用申请就可以使用,我记得以前需要在哪个config的jsApiList里加上这个录音功能权限,你可以看看是不是这个问题,如果不是请无视。。。我手头没有这套环境,不好测试,不好意思。。。

大概明白你为什么要在ready里调用那些接口了,应该是为了让用户刚进入页面就确认是否同意录音权限,防止用户在点击按钮的时候去选择,影响用户体验是吧

我后来把onload里面的wx.ready删了 还是按按钮再确认…

这个应该弹出对话框 让用户允许录音 却一直没有这个对话框出现
导致

注入config的ajax

invalid signature应该是服务器计算这个signature错误的问题,但是你之前说sign true,我不知道是拼错了还是真的成功了,检查一下signature这个值的算法是不是有问题

多谢大佬 原来是注入失败了啊…
英语不好 坑爹了…

你是怎么在cocosCreator调用Wx的东西的

wx本来就在window下,是全局的

请问下 JSSDK是怎么接入的?我现在正在弄H5的微信登录 录音等 求指导

直接引入js文件就可以使用了 或者导入为插件 wx对象是全局的

请问下,微信小程序wx.startRecord 提示没有这个方法,掉用不了,为什么