微信JS接口无法播放录音是怎么回事

移动开发 码拜 9年前 (2016-04-02) 1166次浏览
代码如下

<?php
require_once "jssdk.php";
$jssdk = new JSSDK("本人的AppID", "本人的AppSecret");
$signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <button  id="startRecord">startRecord</button>
    <button  id="stopRecord">stopRecord</button>
    <button  id="playVoice">playVoice</button>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
  wx.config({
       debug: true,
    appId: "<?php echo $signPackage["appId"];?>",
    timestamp: <?php echo $signPackage["timestamp"];?>,
    nonceStr: "<?php echo $signPackage["nonceStr"];?>",
    signature: "<?php echo $signPackage["signature"];?>",
    jsApiList: [
       "checkJsApi",
        "onMenuShareTimeline",
        "onMenuShareAppMessage",
        "onMenuShareQQ",
        "onMenuShareWeibo",
        "hideMenuItems",
        "showMenuItems",
        "hideAllNonBaseMenuItem",
        "showAllNonBaseMenuItem",
        "translateVoice",
        "startRecord",
        "stopRecord",
        "onRecordEnd",
        "playVoice",
        "pauseVoice",
        "stopVoice",
        "uploadVoice",
        "downloadVoice",
        "chooseImage",
        "previewImage",
        "uploadImage",
        "downloadImage",
        "getNetworkType",
        "openLocation",
        "getLocation",
        "hideOptionMenu",
        "showOptionMenu",
        "closeWindow",
        "scanQRCode",
        "chooseWXPay",
        "openProductSpecificView",
        "addCard",
        "chooseCard",
        "openCard"
    ]
  });
  wx.ready(function () {
     document.querySelector("#startRecord").onclick = function () {
    wx.startRecord({
      cancel: function () {
        alert("用户拒绝授权录音");
      }
    });
  };
      // 4.3 停止录音
  document.querySelector("#stopRecord").onclick = function () {
    wx.stopRecord({
      success: function (res) {
        voice.localId = res.localId;
      },
      fail: function (res) {
        alert(JSON.stringify(res));
      }
    });
  };
  // 4.4 监听录音自动停止
  wx.onVoiceRecordEnd({
    complete: function (res) {
      voice.localId = res.localId;
      alert("录音时间已超过一分钟");
    }
  });
      
  // 4.5 播放音频
  document.querySelector("#playVoice").onclick = function () {
    if (voice.localId == "") {
      alert("请先使用 startRecord 接口录制一段声音");
      return;
    }
    wx.playVoice({
      localId:voice.localId 
    });
  };
      
  });
</script>
</html>

都是复制官方的代码的,录音和停止录音都正常使用,用debug模式录音和停止录音都返回了OK。但是播放录音就是不行。这到底是怎么回事啊。

解决方案

5

本人也有同样的问题,求高手解决

5

终于解决了,少定义了一个变量
加在前面就好了
var voice = {
localId: “”,
serverId: “”
};

5

你这个是录音效果实现了是么?

5

题主,代码传上去啊

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明微信JS接口无法播放录音是怎么回事
喜欢 (0)
[1034331897@qq.com]
分享 (0)