首页 课程中心 问答&分享 读书 公众号框架 HUI H.JS 开放源 登录 & 注册
 《微信公众号开发权威教程》

开始录音接口

wx.startRecord();


停止录音接口

wx.stopRecord({
    success: function(res){var localId = res.localId;}
});


监听录音自动停止接口

wx.onVoiceRecordEnd({
    //录音时间超过一分钟没有停止的时候会执行 complete 回调
    complete:function(res){var localId = res.localId;}
});


播放语音接口

wx.playVoice({localId: '' // 需要播放的音频的本地ID,由stopRecord接口获得});


暂停播放接口

wx.pauseVoice({
    localId: '' // 需要暂停的音频的本地ID,由stopRecord接口获得
});


停止播放接口

wx.stopVoice({localId: '' // 需要停止的音频的本地ID,由stopRecord接口获得});


监听语音播放完毕接口

wx.onVoicePlayEnd({
    success: function(res){
        var localId = res.localId;// 返回音频的本地ID
    }
});


上传语音接口

wx.uploadVoice({
    localId: '', // 需要上传的音频的本地ID,由stopRecord接口获得
    isShowProgressTips: 1, // 默认为1,显示进度提示
    success: function (res) {
        var serverId = res.serverId; // 返回音频的服务器端ID
    }
});


识别音频并返回识别结果接口

wx.translateVoice({
    localId: localId, // 需要识别的音频的本地Id,由录音相关接口获得
    isShowProgressTips: 1, // 默认为1,显示进度提示
    success: function (res) {
        alert(res.translateResult); // 语音识别的结果
    }
});


代码演示(注意权限):

<html>
<head>
<title>demo</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
</head>
<body>
<div style="width:90%;"><img src="http://static.hcoder.net/public/course_images/5595e9446293b.jpg" width="100%" /></div>
<div style="padding:15px;"><input type="button" value="开始录音" id="button" /></div>
<div style="padding:15px;"><input type="button" value="停止录音" id="button2" /></div>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript"></script>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.10.2/jquery.js"></script>
<?php
require_once "jssdk.php";
//$jssdk = new JSSDK("appid", "appsecret");
$jssdk = new JSSDK("wxecbdf50f2ffea20f", "eb2e237b0ed8624d4fd87a718ae2285b");
$signPackage = $jssdk->GetSignPackage();
?>
<script>
wx.config({
    debug: false,
    appId: '<?php echo $signPackage["appId"];?>',
    timestamp: <?php echo $signPackage["timestamp"];?>,
    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
    signature: '<?php echo $signPackage["signature"];?>',
    jsApiList: ['startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice']
});
wx.ready(function () {
    $('#button').click(function(){wx.startRecord();});
    $('#button2').click(function(){
        wx.stopRecord({
            success: function (res) {
                var localId = res.localId;
                alert(localId);
                wx.playVoice({localId: localId});
            }
        });
    });
});
wx.error(function(res){alert(res);});
</script>
</html>


书籍目录  (请点击阅读)