微信js-sdk 錄音功能的示例代碼
需求描述
制作一個(gè)H5頁面,打開之后可以錄音,并將錄音文件提交至后臺(tái)





微信錄音最長時(shí)長為1min
代碼如下
// isVoice: 0-未錄音 1-錄音中 2-錄完音
// 點(diǎn)擊錄音/錄音中 按鈕展示
<div class="vm-voice-box" v-show="isVoice < 2">
<p v-show="!isVoice" @click="voiceStart">點(diǎn)擊錄音</p>
<img v-show="isVoice" @click="voiceEnd" src="../../xxx/ico-voice.png" alt="">
</div>
// isListen // 0-未試聽/試聽結(jié)束 1-試聽中 2-暫停試聽
// 錄完音 按鈕展示
<div class="vm-voice-player" v-show="isVoice == 2">
<div class="vm-vp-button">
<p class="vm-vp-revoice" @click="openMask(0)">重錄</p>
<p class="vm-vp-submit" :class="{'vm-vp-no-submit' : isSubmit}" @click="openMask(1)">提交</p>
<p class="vm-vp-pause" v-show="!isListen" @click="play">試聽</p>
<p class="vm-vp-pause" v-show="isListen==1" @click="pause">| |</p>
<p class="vm-vp-pause vm-vp-border" v-show="isListen==2" @click="play"> ▶ </p>
</div>
</div>
data() {
return {
id: '',
startTime: 0,
recordTimer: null,
localId: '', // 錄音本地id
serverId: '', // 錄音微信服務(wù)id
showMask: false,
tip: 1, //提交 0- 重錄
isVoice: 0, // 0-未錄音 1-錄音中 2-錄完音
isListen: 0, // 0-未試聽/試聽結(jié)束 1-試聽中 2-暫停試聽
data1: 0,
work: {},
isPlay: false, // 是否播放
isSubmit: false, // 是否已提交
}
}
// 微信配置
getConfig() {
let _url = encodeURIComponent(window.location.href)
// 后臺(tái)提供接口,傳入當(dāng)前url(返回基礎(chǔ)配置信息)
voiceApi.wechatConfig(_url)
.then(res => {
if (res.data.code == 200) {
wx.config({
debug: false,
appId: res.data.content.appid,
timestamp: res.data.content.timestamp, // 必填,生成簽名的時(shí)間戳
nonceStr: res.data.content.nonceStr, // 必填,生成簽名的隨機(jī)串
signature: res.data.content.signature, // 必填,簽名
// 需要授權(quán)的api接口
jsApiList: [
'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'uploadVoice', 'downloadVoice', 'playVoice', 'pauseVoice', 'onVoicePlayEnd'
]
})
wx.ready( () => {
wx.onVoiceRecordEnd({
// 錄音時(shí)間超過一分鐘沒有停止的時(shí)候會(huì)執(zhí)行 complete 回調(diào)
complete: function (res) {
_this.isVoice = 2
_this.localId = res.localId;
}
})
})
}
})
},
// 開始錄音
voiceStart(event) {
let _this = this
event.preventDefault()
// 延時(shí)后錄音,避免誤操作
this.recordTimer = setTimeout(function() {
wx.startRecord({
success: function() {
_this.startTime = new Date().getTime()
_this.isVoice = 1
},
cancel: function() {
_this.isVoice = 0
}
})
}, 300)
},
// 停止錄音
voiceEnd(event) {
this.isVoice = 2
let _this = this
event.preventDefault()
// 間隔太短
if (new Date().getTime() - this.startTime < 300) {
this.startTime = 0
// 不錄音
clearTimeout(this.recordTimer)
} else {
wx.stopRecord({
success: function(res) {
// 微信生成的localId,此時(shí)語音還未上傳至微信服務(wù)器
_this.localId = res.localId
},
fail: function(res) {
console.log(JSON.stringify(res))
}
})
}
},
// 試聽
tryListen() {
let _this = this
wx.playVoice({
localId: _this.localId // 需要播放的音頻的本地ID,由stopRecord接口獲得
})
console.log('試聽。。。')
wx.onVoicePlayEnd({ // 監(jiān)聽播放結(jié)束
success: function (res) {
console.log('試聽監(jiān)聽結(jié)束')
_this.isListen = 0
}
});
},
// 試聽停止
tryStop() {
let _this = this
wx.pauseVoice({
localId: _this.localId // 需要停止的音頻的本地ID,由stopRecord接口獲得
})
},
// 處理錄音數(shù)據(jù)
voiceHandle() {
let _this = this
wx.uploadVoice({
localId: this.localId, // 需要上傳的音頻的本地ID,由stopRecord接口獲得
isShowProgressTips: 1, // 默認(rèn)為1,顯示進(jìn)度提示
success: function (res) {
// 微信語音已上傳至 微信服務(wù)器并返回一個(gè)服務(wù)器id
_this.serverId = res.serverId; // 返回音頻的服務(wù)器端ID
_this.upVoice()
}
})
},
// 自己后臺(tái)上傳接口
upVoice() {
let data = {
id: this.id,
serviceId: this.serverId
}
voiceApi.upVoice(data)
.then(res => {
if(res.data.code == 200) {
// ??! todo 隱藏loading
this.isSubmit = true
this.$Message.message('提交成功')
this.closeMask()
} else {
this.$Message.message(res.data.message)
}
})
.catch(err => {
console.log(err)
})
},
1. 微信jsdk配置
2. 調(diào)取微信錄音開始方法 wx.startRecord
3. 調(diào)取微信錄音結(jié)束方法 wx.stopRecord
成功后返回一個(gè)本地音頻id localId
⚠️ 如果不調(diào)用錄音結(jié)束方法,待錄音1min后自動(dòng)結(jié)束,需要wx.onVoiceRecordEnd 監(jiān)聽錄音結(jié)束
4. 上傳錄音至微信服務(wù)器 wx.uploadVoice
返回serverId
⚠️ 微信存儲(chǔ)時(shí)間有限,有效期3天
⚠️ 目前多媒體文件下載接口的頻率限制為10000次/天,如需要調(diào)高頻率,請登錄微信公眾平臺(tái),在開發(fā) - 接口權(quán)限的列表中,申請?zhí)岣吲R時(shí)上限。
5. 調(diào)取自己后臺(tái)上傳至自己服務(wù)器
這部可以看做,將 serverId 傳給自己的服務(wù)器,然后自己服務(wù)器調(diào)微信提供的接口去下載(serverId)至自己服務(wù)器存儲(chǔ)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript實(shí)現(xiàn)校驗(yàn)文件上傳控件實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)校驗(yàn)文件上傳控件,實(shí)例分析了javascript檢測上傳文件類型是否為圖片的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
JavaScript實(shí)現(xiàn)重力下落與彈性效果的方法分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)重力下落與彈性效果的方法,結(jié)合實(shí)例形式分析了javascript重力下落及彈性效果的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
javascript面向?qū)ο笕筇卣髦鄳B(tài)實(shí)例詳解
這篇文章主要介紹了javascript面向?qū)ο笕筇卣髦鄳B(tài),結(jié)合實(shí)例形式詳細(xì)分析了javascript面向?qū)ο蟪绦蛟O(shè)計(jì)中多態(tài)的概念、原理,并結(jié)合實(shí)例形式總結(jié)了多態(tài)的實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2019-07-07
css+js實(shí)現(xiàn)部分區(qū)域高亮可編輯遮罩層
下面介紹我在項(xiàng)目中實(shí)現(xiàn)的方式,全屏遮罩,部分區(qū)域可操作,需要的朋友可以參考下2014-03-03
javascript內(nèi)置對象Date案例總結(jié)分析
今天總結(jié)javascript內(nèi)置對象Date的使用,并且寫一個(gè)重要的網(wǎng)頁倒計(jì)時(shí)的核心算法案例,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
基于JavaScript實(shí)現(xiàn)復(fù)選框的全選和取消全選
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)復(fù)選框的全選和取消全選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

