- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- 微信小程序audio組件在ios端無(wú)法播放的解決辦法
解決方法: 給 audio 組件綁定點(diǎn)擊事件,手動(dòng)觸發(fā)播放暫停方法!
代碼片段:
wxml文件
<!-- 判斷是語(yǔ)音通話(huà),有通話(huà)記錄,通話(huà)描述不包含'未接' --> <view class="reference" wx:if="{{itemList.activity_type === 'phone' && itemList.activity_reference_id && tool.indexOf(itemList.comment,'未接') === -1 }}"> <!-- 語(yǔ)音播放 --> <van-button class="ref-btn" wx:if="{{audioResourceMaps[itemList.activity_reference_id] === undefined}}" loading="{{itemList.activity_reference_id === currentGettingReferenceId}}" icon="play" type="info" plain data-reference-id="{{itemList.activity_reference_id}}" bindtap="getReference"> </van-button> <view wx:else class="audio-box"> <!-- 語(yǔ)音播放暫停 --> <van-button class="ref-btn" wx:if="{{audioResourceMaps[itemList.activity_reference_id]}}" data-reference-id="{{itemList.activity_reference_id}}" icon="pause" type="info" plain bindtap="pauseAudio"/> <!-- 點(diǎn)擊沒(méi)有通話(huà)錄音 --> <span wx:else class="no-audio-text">未找到通話(huà)錄音</span> </view> </view>
wxss文件
.reference { margin-top: 20rpx; height: 100%; padding: 5rpx; box-sizing: border-box; } .ref-btn { width: 80rpx; height: 80rpx; display: flex; } .ref-btn button { width: 80rpx; height: 80rpx; border-radius: 50%; }
js文件
/** * 組件的初始數據 */ data: { currentGettingReferenceId: null, //正在播放的音頻id audioResourceMaps: {}, //點(diǎn)擊過(guò)的音頻列表 isPause:false, // 是否暫停 }, /** * 組件的生命周期 */ lifetimes: { attached: function () { // 因為是子組件 所以要在這里獲取實(shí)例 this.audioContext = wx.createInnerAudioContext(); }, detached: function () { // 停止播放 this.stopAudio() // 在組件實(shí)例被從頁(yè)面節點(diǎn)樹(shù)移除時(shí)執行 }, }, methods: { // 獲取錄音 getReference(e) { let id = e.target.dataset.referenceId if(id != this.data.currentGettingReferenceId){ this.stopAudio() } this.setData({ currentGettingReferenceId:id }) // 點(diǎn)擊獲取錄音url的接口。 接口請求根據自己的封裝來(lái)寫(xiě) WXAPI.getResourceUrl( `/conversation/conversationsession/${id}/`, { data_type: 'all', }).then(({resource_url}) => { console.log('音頻地址====',resource_url,) let url = resource_url && resource_url.indexOf('https://') > -1? encodeURI(resource_url) : null this.data.audioResourceMaps[id] = url; if(resource_url) this.playAudio(id,url) this.setData({ audioResourceMaps: this.data.audioResourceMaps }) console.log('播放過(guò)的列表=====',this.data.audioResourceMaps) }).catch(function (e) { console.log(e) }) }, // 暫停 pauseAudio(){ this.setData({ isPause: !this.data.isPause }) let id = this.data.currentGettingReferenceId console.log(id,'播放暫停的id') const innerAudioContext = this.audioContext if(this.data.isPause){ innerAudioContext.pause() console.log('暫停播放') }else{ innerAudioContext.play() console.log('繼續播放') } }, // 停止播放 stopAudio(){ const innerAudioContext = this.audioContext innerAudioContext.stop() let obj = this.data.audioResourceMaps for(let item in obj){ delete obj[item] } // 停止播放就要把播放列表id對應的音頻地址做清空處理 this.setData({ audioResourceMaps: obj, currentGettingReferenceId:null }) console.log('停止播放') }, // 播放 playAudio(id,url) { const innerAudioContext = this.audioContext console.log(url, '音頻的地址') if(url){ innerAudioContext.src = url innerAudioContext.play() innerAudioContext.onPlay(() => { console.log('開(kāi)始播放') }) innerAudioContext.onTimeUpdate(() => { console.log(innerAudioContext.duration,'總時(shí)長(cháng)') console.log(innerAudioContext.currentTime,'當前播放進(jìn)度') }) setTimeout(() => { console.log(innerAudioContext.duration,'總時(shí)長(cháng)') console.log(innerAudioContext.currentTime,'當前播放進(jìn)度') }, 500) innerAudioContext.onEnded(() => { let obj = this.data.audioResourceMaps for(let item in obj){ delete obj[item] } this.setData({ audioResourceMaps: obj, currentGettingReferenceId:null }) console.log('播放完畢') }) innerAudioContext.onError((res) => { console.log(res.errMsg) console.log(res.errCode) }) } }
效果圖
⚠️微信小程序中使用vant,必須要在.json 文件中引用 不然標簽不會(huì )顯示哦
我是在app.json文件引得 全局可用
"usingComponents": { "van-button": "@vant/weapp/button/index", "van-icon": "@vant/weapp/icon/index", }
官網(wǎng)文檔:…
總結
到此這篇關(guān)于微信小程序audio組件在ios端無(wú)法播放解決的文章就介紹到這了,更多相關(guān)小程序audio組件ios端播放內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站