微信小程序如何播放音頻,這里以本地mp3文件舉例。


參考:https://blog.csdn.net/bangyiqing/article/details/104966135      官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html

 

Page({
    data: {
        show: true,
        current: 0,
        playTwo: true,
    },
    // 點擊領取禮包
    getward: function() {
        this.setData({
            show: false
        })
    },
    // 點擊在看一次
    gofirst: function() {
        this.setData({
            show: true,
            current: 0
        })
    },
    // 音樂圖標旋轉
    playTwo: function() {
        this.setData({
            playTwo: !this.data.playTwo
        })
        if (this.data.playTwo) {
            this.data.innerAudioContext.play();
        } else {
            this.data.innerAudioContext.pause();
        }
    },
    handleChange: function(event) {
        console.log(event.detail)
        if (event.detail.source === "touch") {
            this.setData({
                current: event.detail.current
            })
        }
    },
    playmp3: function(event) {
        let srcurl = "/packageC/activety/小啊Giao.mp3"
        this.data.innerAudioContext.autoplay = true
        this.data.innerAudioContext.src = srcurl
        this.data.innerAudioContext.onPlay(() => {
            console.log('開始播放')
        })
        this.mp3stop()
    },
    // 監聽音頻停止事件
    mp3stop: function(e) {
        let that = this
        that.data.innerAudioContext.onEnded((res) => {
            that.data.innerAudioContext.play();
        })
    },
    onLoad: function(options) {
        this.setData({
            innerAudioContext: wx.createInnerAudioContext()
        })
    },
    onReady: function() {},
    onShow: function() {
        this.playmp3()
    },
    onHide: function() {
        // this.data.innerAudioContext.destroy()
    },
    onUnload: function() {
        this.data.innerAudioContext.destroy()
    },
    onPullDownRefresh: function() {
        //Do some when page pull down.

    }
})

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM