audio.js
5.44 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const bgMusic = wx.createInnerAudioContext();
const app = getApp();
Component({
options: {
addGlobalClass: true,
},
/**
* 组件的属性列表
*/
properties: {
//音频地址从父组件获取
address: {
type: String,
value: "",
observer: function (n, o) {
bgMusic.src = n
console.log(bgMusic.src)
}
},
durationTime: {
type: String,
value: "",
observer: function (n, o) {
var duration = this.formatTime(n);
this.setData({
duration,
offset: 0
// max: parseInt(n / 1000)
});
}
},
},
// ob
/**
* 组件的初始数据
*/
data: {
offset: 0,
max: 99999,
isOpen: false, //播放开关
starttime: "00:00", //正在播放时长
duration: "00:20", //总时长
},
lifetimes: {
attached() {
// init()
this.audioInit()
},
detached() {
// console.log(bgMusic.src);
// bgMusic.src = ''
bgMusic.stop()
this.setData({
offset: 0,
starttime: "00:00", //正在播放时长
})
// bgMusic.destroy()
}
},
/**
* 组件的方法列表
*/
methods: {
audioInit() {
let that = this
// 设置音频播放倍速,此处若不设置,页面上点击设置倍速就不会产生效果
bgMusic.src = this.data.address
console.log(bgMusic.src, this.data.address);
bgMusic.playbackRate = 1.0
// 播放监听
bgMusic.onPlay(() => {
console.log('开始播放')
console.log(bgMusic.playbackRate);
})
// 暂停监听
bgMusic.onPause(() => {
console.log('停止播放')
})
// 监听音频进入可以播放状态的事件。但不保证后面可以流畅播放,必须要这个监听,不然播放时长更新监听不会生效,不能给进度条更新值
bgMusic.onCanplay(() => {
bgMusic.duration
})
// 播放时长更新监听
bgMusic.onTimeUpdate(() => {
//bgMusic.duration总时长 bgMusic.currentTime当前进度
console.log(bgMusic.currentTime);
var duration = this.formatTime(this.data.durationTime);
var offset = bgMusic.currentTime;
var currentTime = parseInt(bgMusic.currentTime);
var min = "0" + parseInt(currentTime / 60);
var max = parseInt(bgMusic.duration);
var sec = currentTime % 60;
if (sec < 10) {
sec = "0" + sec;
}
var starttime = min + ":" + sec; /* 00:00 */
that.setData({
offset: currentTime,
starttime: starttime,
max: max,
changePlay: true,
});
});
// 播放出错监听
bgMusic.onError((res) => {
// this.audioInit()
// this.listenerButtonPlay()
console.log(res);
console.log(res.errMsg)
console.log(res.errCode)
})
//播放结束
bgMusic.onEnded(() => {
that.setData({
starttime: "00:00",
isOpen: false,
offset: 0,
});
console.log("音乐播放结束");
});
},
//播放
listenerButtonPlay: function () {
var that = this;
// if (!bgMusic.src) {
// //bug ios 播放时必须加title 不然会报错导致音乐不播放
// // bgMusic.title = "录音";
// // bgMusic.epname = "录音";
// bgMusic.src =
// that.data.address ||
// "https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/17173075357941717307224689.mp3";
// bgMusic.onTimeUpdate(() => {
// //bgMusic.duration总时长 bgMusic.currentTime当前进度
// console.log(bgMusic.currentTime);
// var duration = this.formatTime(this.data.durationTime);
// var offset = bgMusic.currentTime;
// var currentTime = parseInt(bgMusic.currentTime);
// var min = "0" + parseInt(currentTime / 60);
// var max = parseInt(bgMusic.duration);
// var sec = currentTime % 60;
// if (sec < 10) {
// sec = "0" + sec;
// }
// var starttime = min + ":" + sec; /* 00:00 */
// that.setData({
// offset: currentTime,
// starttime: starttime,
// max: max,
// changePlay: true,
// });
// });
// }
if (!bgMusic.src) {
bgMusic.src = this.data.address
}
console.log(bgMusic.src, this.data.address);
bgMusic.play();
that.setData({
isOpen: true,
});
},
//暂停播放
listenerButtonPause() {
var that = this;
bgMusic.pause();
that.setData({
isOpen: false,
});
},
listenerButtonStop() {
var that = this;
bgMusic.stop();
},
// 进度条拖拽
sliderChange(e) {
var that = this;
var offset = parseInt(e.detail.value);
bgMusic.play();
bgMusic.seek(offset);
that.setData({
isOpen: true,
});
},
formatTime(ms) {
const totalSeconds = Math.floor(ms / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes.toString().padStart(2, "0")}:${seconds
.toString()
.padStart(2, "0")}`;
},
},
});