初始化
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
|
||||
// 创建音频实例
|
||||
const audioCtx = wx.createInnerAudioContext();
|
||||
|
||||
Component({
|
||||
/**
|
||||
* 组件的属性列表
|
||||
*/
|
||||
properties: {
|
||||
//音乐地址(网络地址或者根路径/开头的本地音乐地址)
|
||||
url: {
|
||||
type: String,
|
||||
value: ''
|
||||
},
|
||||
//是否自动播放
|
||||
autoplay: {
|
||||
type: Boolean,
|
||||
value: false
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 组件的初始数据
|
||||
*/
|
||||
data: {
|
||||
audioCtx: null,
|
||||
isPlay: false,
|
||||
animationData: {},
|
||||
timer: null,
|
||||
index: 0,
|
||||
},
|
||||
lifetimes: {
|
||||
attached: function () {
|
||||
let that = this
|
||||
// 在组件实例进入页面节点树时执行
|
||||
// 设置音频属性
|
||||
audioCtx.src = this.data.url;
|
||||
audioCtx.autoplay = this.data.autoplay;
|
||||
audioCtx.loop = true;
|
||||
// 旋转动画对象
|
||||
const animation = wx.createAnimation({
|
||||
duration: 60,
|
||||
timingFunction: 'linear',
|
||||
delay: 0,
|
||||
transformOrigin: '50% 50% 0'
|
||||
});
|
||||
this.setData({
|
||||
animation
|
||||
})
|
||||
if (this.data.autoplay) {
|
||||
this.startAnimationInterval()
|
||||
}
|
||||
//监听播放结束事件
|
||||
audioCtx.onEnded(function () {
|
||||
console.log('我播放结束了');
|
||||
if (that.data.timer) {
|
||||
clearInterval(that.data.timer)
|
||||
}
|
||||
})
|
||||
},
|
||||
detached: function () {
|
||||
// 在组件实例被从页面节点树移除时执行
|
||||
clearInterval(this.data.timer)
|
||||
audioCtx.pause()
|
||||
},
|
||||
moved(){
|
||||
console.log(2000);
|
||||
},
|
||||
hide(){
|
||||
console.log(2000);
|
||||
}
|
||||
},
|
||||
pageLifetimes: {
|
||||
show: function() {
|
||||
// 页面被展示
|
||||
},
|
||||
hide: function() {
|
||||
// 页面被隐藏
|
||||
audioCtx.pause()
|
||||
clearInterval(this.data.timer)
|
||||
},
|
||||
},
|
||||
observers: {
|
||||
|
||||
},
|
||||
/**
|
||||
* 组件的方法列表
|
||||
*/
|
||||
methods: {
|
||||
musicClick() {
|
||||
if (audioCtx.paused) {
|
||||
this.startAnimationInterval()
|
||||
audioCtx.play();
|
||||
} else {
|
||||
clearInterval(this.data.timer)
|
||||
this.setData({
|
||||
timer: null
|
||||
})
|
||||
audioCtx.pause();
|
||||
}
|
||||
},
|
||||
// 开始旋转
|
||||
startAnimationInterval() {
|
||||
let inde = this.data.index
|
||||
let timer = setInterval(function () {
|
||||
inde++
|
||||
this.data.animation.rotate(10 * inde).step()
|
||||
this.setData({
|
||||
animationData: this.data.animation.export(),
|
||||
index: inde
|
||||
})
|
||||
}.bind(this), 60)
|
||||
this.setData({
|
||||
timer
|
||||
})
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
<view class="music" bindtap="musicClick">
|
||||
<image class="image" animation="{{animationData}}" src="https://pxboss.xiaohebang.cn/system/default/music.png" mode="aspectFit" />
|
||||
</view>
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
.image{
|
||||
width: 80rpx;
|
||||
height: 80rpx;
|
||||
}
|
||||
Reference in New Issue
Block a user