微信小程序-音频播放-wx.createInnerAudioContext() 每次都是重复播放同一条录音
生活随笔
收集整理的這篇文章主要介紹了
微信小程序-音频播放-wx.createInnerAudioContext() 每次都是重复播放同一条录音
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
在調(diào)試微信小程序音頻播放時,剛開始我也是直接復(fù)制官方文檔的實(shí)例:
const innerAudioContext = wx.createInnerAudioContext() innerAudioContext.autoplay = true innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46' innerAudioContext.onPlay(() => {console.log('開始播放') }) innerAudioContext.onError((res) => {console.log(res.errMsg)console.log(res.errCode) })然而此時就出現(xiàn)了,無論按多少下都是重復(fù)播放同一條語音的情況!
從 https://blog.csdn.net/qq_39364032/article/details/79742120?得知,wx.createInnerAudioContext()這個api新建了一個實(shí)例之后,需要銷毀實(shí)例,才能重新new一個實(shí)例。所以,我做了以下改動:
const innerAudioContext = wx.createInnerAudioContext()innerAudioContext.autoplay = trueinnerAudioContext.src = res.tempFilePathinnerAudioContext.onPlay(() => {console.log('開始播放')})innerAudioContext.onStop(() => {console.log('i am onStop')innerAudioContext.stop()//播放停止,銷毀該實(shí)例innerAudioContext.destroy()})innerAudioContext.onEnded(() => {console.log('i am onEnded')//播放結(jié)束,銷毀該實(shí)例innerAudioContext.destroy()console.log('已執(zhí)行destory()')})innerAudioContext.onError((res) => {console.log(res.errMsg)console.log(res.errCode)innerAudioContext.destroy()})最終解決方案
然而!無論我執(zhí)行多少次,還是播放第一條語音!從我的后臺來看,每次返回的語音文件都是不同的,甚至我把服務(wù)器上的文件下載到本地,也是不同的!于是我就懷疑它下載過一次這個地址就不再下載了。所以我又換一種寫法,在上面加上wx.downloadFile這個api。
wx.downloadFile({url: 'http://47.106.74.22:8081/voice/download_audio', //僅為示例,并非真實(shí)的資源success: function (res) {// 只要服務(wù)器有響應(yīng)數(shù)據(jù),就會把響應(yīng)內(nèi)容寫入文件并進(jìn)入 success 回調(diào),業(yè)務(wù)需要自行判斷是否下載到了想要的內(nèi)容if (res.statusCode === 200) {const innerAudioContext = wx.createInnerAudioContext()innerAudioContext.autoplay = trueinnerAudioContext.src = res.tempFilePathinnerAudioContext.onPlay(() => {console.log('開始播放')})innerAudioContext.onStop(() => {console.log('i am onStop')innerAudioContext.stop()//播放停止,銷毀該實(shí)例innerAudioContext.destroy()})innerAudioContext.onEnded(() => {console.log('i am onEnded')//播放結(jié)束,銷毀該實(shí)例innerAudioContext.destroy()console.log('已執(zhí)行destory()')})innerAudioContext.onError((res) => {console.log(res.errMsg)console.log(res.errCode)innerAudioContext.destroy()})}}})到這里,終于解決這個問題
注意的是,wx.createInnerAudioContext()無法播放wav文件,于是這里我用的是MP3文件。而wx.playVoice(OBJECT)也無法播放mp3文件。
?
轉(zhuǎn)載于:https://www.cnblogs.com/whomhim/p/10518692.html
總結(jié)
以上是生活随笔為你收集整理的微信小程序-音频播放-wx.createInnerAudioContext() 每次都是重复播放同一条录音的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wps如何将字体竖着排列_WPS文字中怎
- 下一篇: 001_iBase4J学习之环境搭建