music.js
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// components/music/music.js
Component({
/**
* 组件的属性列表
*/
properties: {
audioSrc: {
type: String
},
audioName: {
type: String
},
audioTitle: {
type: String
},
},
/**
* 组件的初始数据
*/
data: {
audioPlay: false, //当前的播放状态控制
sliderValue: 0, //进度条最小值
sliderMax: 100, //进度条最大值
backgroundAudioManager: "", //播放实例
currentTimeStr: "00:00", //当前进度的时间
durationTimeStr: "00:00", //总的时间
changeValState: false //在拉动进度条的时候,拉动完成的状态
},
/**
* 组件的方法列表
*/
methods: {
creatAudio: function () {
let that = this;
that.data.backgroundAudioManager = wx.getBackgroundAudioManager();
that.data.backgroundAudioManager.title = '音频';
that.data.backgroundAudioManager.onPlay(function () { //播放监听
console.log('播放!');
that.setData({
audioPlay: true
})
})
that.data.backgroundAudioManager.onPause(function () { //暂停监听
console.log('暂停播放!');
that.setData({
audioPlay: false
})
})
that.data.backgroundAudioManager.onEnded(function () { //结束播放监听
console.log('播放结束!');
that.setData({
sliderValue:0,
audioPlay: false
})
})
that.data.backgroundAudioManager.onError(function (res) { // 播放失败监听
console.log('播放音频失败', res);
})
that.data.backgroundAudioManager.onSeeked(function (res) { //监听结束跳转事件callback(无效)
console.log("结束跳转", res);
that.setData({
changeValState: false
})
})
that.data.backgroundAudioManager.onTimeUpdate(function (res) { //监听背景音频播放进度更新事件
if (that.data.changeValState) return false;
let currentTime = that.data.backgroundAudioManager.currentTime;
let duration = that.data.backgroundAudioManager.duration;
if (that.data.sliderMax == 100) {
that.setData({
sliderMax: duration.toFixed(0)
})
}
let val = parseInt((currentTime / duration) * that.data.sliderMax);
that.setData({
sliderValue: val
})
let currTimeStr = that.formatTime(currentTime);
let duraTimeStr = that.formatTime(duration);
that.setData({
currentTimeStr: currTimeStr,
durationTimeStr: duraTimeStr
})
})
},
//滑动条改变事件
sliderChange: function (e) {
let that = this;
console.log(e);
that.setData({
sliderValue: e.detail.value
})
if (that.data.backgroundAudioManager == "") return false;
that.setData({
changeValState: true
})
that.data.backgroundAudioManager.seek(e.detail.value);
setTimeout(function () {
that.setData({
changeValState: false
})
}, 500)
},
//播放按钮点击事件
changePlayState: function () {
let that = this;
this.setData({
audioPlay: !this.data.audioPlay
})
if (this.data.audioPlay) {
if (!that.data.backgroundAudioManager.src) { //初始化给backgroundAudioManager.src赋值
that.creatAudio();
that.data.backgroundAudioManager.src = that.data.audioSrc; //当设置了src后会自动播放
}
that.data.backgroundAudioManager.play();
} else {
that.data.backgroundAudioManager.pause();
}
},
formatTime: function (num) { //格式化时间格式
num = num.toFixed(0);
let second = (num % 60);
if (second < 10) second = '0' + second;
let min = Math.floor(num / 60);
if (min < 10) min = '0' + min;
return min + ":" + second;
}
}
})