微信小程序中音频播放控件
官方文档:https://mp.weixin.qq.com/debug/wxadoc/dev/component/audio.html
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;
}
本人菜鸟一个,有什么 不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!
孜孜不倦,必能求索;风尘仆仆,终有归途。