W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:銷毀當(dāng)前實(shí)例
無
<view class="wrap">
<view class="card-area">
<button type="primary" bindtap="destroy">創(chuàng)建并銷毀一個(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è)頁面有兩種形式的語音播放,如:一個(gè)是播放語音列表里的語音,一個(gè)是預(yù)聽待提交的語音。這兩種的onPlay和onEnd回調(diào)內(nèi)部執(zhí)行的不一樣,不可能在onLoad里面用同一個(gè)回調(diào),所以需要?jiǎng)?chuàng)建兩個(gè)innerAudioContext實(shí)例對象時(shí),可在當(dāng)前音頻播放結(jié)束的onEnd的回調(diào)事件里面和音頻播放錯(cuò)誤onError回調(diào)事件里,調(diào)用destory方法銷毀該實(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: '銷毀實(shí)例成功'
});
this.innerAudioContext.destroy();
});
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: