1. 程式人生 > >微信小程式—audio(音訊)

微信小程式—audio(音訊)

今天來介紹一下微信小程式中音訊播放控制元件–audio

1.效果圖如下,

這裡寫圖片描述

2.index.js中:


//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中:

<!-- 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;
}

本人菜鳥一個,有什麼 不對的地方希望大家指出評論,大神勿噴,希望大家一起學習進步!