uniapp使用uni.createInnerAudioContext播放mp3音频

851 阅读1分钟

使用uni.createInnerAudioContext创建播放器对象

主要代码:

audio(string, token) {
    let that = this
    //创建播放器对象
    that.musicObj = uni.createInnerAudioContext();
    //src为播放器的播放路径
    that.musicObj.src='/static/audio/test.mp3'
    //play()为播放的方法
    that.musicObj.play()
    that.musicObj.onPlay(() => {
            console.log('开始播放')
    });
    that.musicObj.onEnded(() => {
            console.log('语音播报结束')
            that.musicObj.destroy() // 销毁
    });
    that.musicObj.onError(() => {
            console.log('语音播报失败')
            that.musicObj.destroy() // 销毁
    });
},