微信小程序中音頻播放控件

【轉】今天來介紹一下微信小程序中音頻播放控件–audiojavascript

官方文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/component/audio.htmlcss

1.效果圖以下,html

這裏寫圖片描述

2.index.js中:java

//index.js
//獲取應用實例
const app = getApp()
var isLoop = true;
var isControls = true;
Page({
  onReady: function (e) {
    // 使用 wx.createAudioContext 獲取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  data: {
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
    name: '此時此刻',
    author: '許巍',
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    controls: true
  },

  onLoad: function () {


  },
  //開始播放
  audioPlay: function () {
    this.audioCtx.play()
  },
  //暫停播放
  audioPause: function () {
    this.audioCtx.pause()
  },
  //設置進度到57秒
  audio57: function () {
    this.audioCtx.seek(57)
  },
  //從新開始播放
  audioStart: function () {
    this.audioCtx.seek(0)
  },
  //設置是否輪播播放
  audioLoop: function () {
    if (isLoop == true) {
      isLoop = false;
      this.setData({
        loop: true,
        controls: true
      })
    } else {
      isLoop = true;
      this.setData({
        loop: false,
        controls: false
      })
    }
  },
  //設置是否顯示默認控件
  audioControls:function(){
    if (isControls == true){
      isControls = false;
      this.setData({
        controls:false
      })
    }else{
      isControls = true;
      this.setData({
        controls: true
      })
    }
  }

})

3.index.wxml中:express

<!-- audio.wxml --> <audio class="audio" poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls="{{controls}}" loop="{{loop}}"></audio> <button class="btn" type="primary" bindtap="audioPlay">播放</button> <button class="btn" type="primary" bindtap="audioPause">暫停</button> <button class="btn" type="primary" bindtap="audio57">設置當前播放時間爲57秒</button> <button class="btn" type="primary" bindtap="audioStart">從頭開始</button> <button class="btn" type="primary" bindtap="audioLoop">設置/取消循環播放</button> <button class="btn" type="primary" bindtap="audioControls">顯示/取消默認控件</button> 

4.index.wxss中:小程序

.btn{ margin-top: 15rpx; }

.audio{ margin-left: 65rpx; }

本人菜鳥一個,有什麼 不對的地方但願你們指出評論,大神勿噴,但願你們一塊兒學習進步!微信小程序

相關文章
相關標籤/搜索