W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋?zhuān)轰N(xiāo)毀當(dāng)前實(shí)例
無(wú)
<view class="wrap">
<view class="card-area">
<button type="primary" bindtap="destroy">創(chuàng)建并銷(xiāo)毀一個(gè)新實(shí)例</button>
</view>
</view>
Page({
onLoad() {
const innerAudioContext = swan.createInnerAudioContext();
innerAudioContext.src = 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3';
innerAudioContext.autoplay = true;
},
destroy() {
// 在實(shí)際項(xiàng)目中,若一個(gè)頁(yè)面有兩種形式的語(yǔ)音播放,如:一個(gè)是播放語(yǔ)音列表里的語(yǔ)音,一個(gè)是預(yù)聽(tīng)待提交的語(yǔ)音。這兩種的onPlay和onEnd回調(diào)內(nèi)部執(zhí)行的不一樣,不可能在onLoad里面用同一個(gè)回調(diào),所以需要?jiǎng)?chuàng)建兩個(gè)innerAudioContext實(shí)例對(duì)象時(shí),可在當(dāng)前音頻播放結(jié)束的onEnd的回調(diào)事件里面和音頻播放錯(cuò)誤onError回調(diào)事件里,調(diào)用destory方法銷(xiāo)毀該實(shí)例。
const createInnerAudioContextTask = new Promise((resolve, reject) => {
const innerAudioContext = swan.createInnerAudioContext();
innerAudioContext.src = 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3';
innerAudioContext.autoplay = false;
innerAudioContext.onEnded(() => {
innerAudioContext.destroy();
});
innerAudioContext.play();
innerAudioContext.onPlay(res => {
resolve();
console.log('onPlay', res);
});
this.innerAudioContext = innerAudioContext;
})
.then(destroyTask => {
swan.showModal({
title: 'destroy',
content: '銷(xiāo)毀實(shí)例成功'
});
this.innerAudioContext.destroy();
});
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: