InnerAudioContext.destroy

2020-08-26 16:27 更新

解释:销毁当前实例

方法参数

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <button type="primary" bindtap="destroy">创建并销毁一个新实例</button>
    </view>
</view>
Page({
    onLoad() {
        const innerAudioContext = swan.createInnerAudioContext();
        innerAudioContext.src = 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3';
        innerAudioContext.autoplay = true;
    },
    destroy() {
        // 在实际项目中,若一个页面有两种形式的语音播放,如:一个是播放语音列表里的语音,一个是预听待提交的语音。这两种的onPlay和onEnd回调内部执行的不一样,不可能在onLoad里面用同一个回调,所以需要创建两个innerAudioContext实例对象时,可在当前音频播放结束的onEnd的回调事件里面和音频播放错误onError回调事件里,调用destory方法销毁该实例。
        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: '销毁实例成功'
            });
            this.innerAudioContext.destroy();
        });
    }
});


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号