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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<template>
<div class="audioOff">
<div class="ofplayBox">
<audio id="audio" :src="src" @canplay ="onplay" @play="playOn"></audio>
<div class="isPlayImg">
<!--开始播放-->
<img v-if="!playflg" @click="play()" :src="playUrl" />
<!--暂停-结束播放-->
<img v-if="playflg" @click="stop()" :src="stopUrl" />
</div>
<!--slider进度条-->
<div class="progress" @mousedown="stopTime()">
<input type="range" min="0" :max="offset" v-model="rangeValue" />
</div>
<div class="duration"><span>{{starttime}}</span>/<span class="right-timer">{{duration}}</span></div>
<div class="cover"></div>
</div>
</div>
</template>
<script>
import {getwechatParam} from "../../service/api";
import stopUrl from '../../assets/mould/look1/rs.png'
import playUrl from '../../assets/mould/look1/rp.png'
//--创建页面监听,页面加载完毕--触发音频播放
export default {
name: 'audioMsg',
props:[
'src',
'autoPlay'
],
data() {
return {
stopUrl:stopUrl,
playUrl:playUrl,
//这首歌时间有点长 大家测试的时候可以换一个短点的MP3文件
playflg: false,
rangeValue: 0,
starttime: '00:00', //正在播放时长
duration: '00:00', //总时长
offset: 0,
timer:null,
first:true
}
},
mounted(){
console.log('触发')
this.play();
if(this.autoPlay){
let that = this;
document.addEventListener('DOMContentLoaded', function () {
if(that.first){
function audioAutoPlay() {
var musicEle0 = document.getElementById('audio');
musicEle0.play();
}
audioAutoPlay();
}
});
//--创建触摸监听,当浏览器打开页面时,触摸屏幕触发事件,进行音频播放
document.addEventListener('touchstart', function () {
if(that.first){
function audioAutoPlay() {
var musicEle0 = document.getElementById('audio');
musicEle0.play();
}
audioAutoPlay();
}
});
this.autoPlayAudio();
this.play();
getwechatParam({
api_list:'onMenuShareAppMessage',
url:window.location.href.split('#')[0]
}).then(res=>{
let that = this;
wx.config({
debug: false,
appId: res.appId,
timestamp: parseInt(res.timestamp),
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: res.jsApiList
});
this.autoPlayAudio();
this.play();
});
this.$nextTick(()=>{
this.first = false
})
}
},
methods: {
stopTime(){
clearInterval(this.timer);
},
autoPlayAudio(){
let that = this;
let voice = document.querySelector('#audio');
document.addEventListener("WeixinJSBridgeReady",function(){
that.play();
},false);
wx.ready(function () {
that.$nextTick(()=> {
wx.ready(function () {
that.$nextTick(()=>{
if (typeof WeixinJSBridge === "object" && typeof WeixinJSBridge.invoke === "function") {
voice.play();
} else {
//監聽客户端抛出事件"WeixinJSBridgeReady"
if (document.addEventListener) {
document.addEventListener("WeixinJSBridgeReady", function(){
voice.play();
}, false);
} else if (document.attachEvent) {
document.attachEvent("WeixinJSBridgeReady", function(){
voice.play();
});
document.attachEvent("onWeixinJSBridgeReady", function(){
voice.play();
});
}
}
wx.ready(()=>{
voice.play();
})
})
})
})
})
},
getDuration(){
let min = null;
if(parseInt(this.offset / 60)<10){
min = '0' + parseInt(this.offset / 60)
}else{
min = parseInt(this.offset / 60)
}
let sec = parseInt(this.offset % 60);
if (sec < 10) {
sec = "0" + sec;
}
this.duration = min + ':' + sec; /* 00:00 */
},
onplay(){
let audio = document.querySelector('#audio');
this.offset = Math.ceil(parseInt(audio.duration));
this.$nextTick(()=>{
this.getDuration();
})
},
playOn(){
let audio = document.querySelector('#audio');
this.playflg = true;
this.timer = setInterval(() => {
let min = null;
if(parseInt(audio.currentTime / 60)<10){
min = '0' + parseInt(audio.currentTime / 60)
}else{
min = parseInt(audio.currentTime / 60)
}
let sec = parseInt(audio.currentTime % 60);
if (sec < 10) {
sec = "0" + sec;
}
this.starttime = min + ':' + sec; /* 00:00 */
this.rangeValue = parseInt(audio.currentTime);
if(this.rangeValue === this.offset) {
this.rangeValue=0;/*播放结束后进度条归零*/
this.starttime='00:00'; /*播放结束后时间归零*/
this.stop();
clearInterval(this.timer)
}
}, 1000)
},
//开始播放
play() {
let audio = document.querySelector('#audio');
audio.play();
},
//暂停-结束
stop() {
let audio = document.querySelector('#audio');
audio.pause();
this.playflg = false
},
},
watch:{
'rangeValue'(value,value2){
let audio = document.querySelector('#audio');
if(Math.abs(value - value2) > 2){
this.timer = setInterval(() => {
let min = null;
if(parseInt(audio.currentTime / 60)<10){
min = '0' + parseInt(audio.currentTime / 60)
}else{
min = parseInt(audio.currentTime / 60)
}
let sec = parseInt(audio.currentTime % 60);
if (sec < 10) {
sec = "0" + sec;
}
this.starttime = min + ':' + sec; /* 00:00 */
this.rangeValue = parseInt(audio.currentTime);
if(this.rangeValue == this.offset) {
this.rangeValue=0;/*播放结束后进度条归零*/
this.starttime='00:00'; /*播放结束后时间归零*/
this.stop()
clearInterval(this.timer)
}
}, 1000);
audio.currentTime = value;
}
}
}
}
</script>
<style lang="less" scoped>
@import "../../util/public";
input[type=range] {
-webkit-transform: translateZ(0);
transform: translateZ(0);
-webkit-appearance: none;
display: flex;
width: 90%;
padding: 0 5 * @toVw;
border-radius: 10px; /*这个属性设置使填充进度条时的图形为圆角*/
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type=range]::-webkit-slider-runnable-track {
height: 2 * @toVw;
background: #999999;
border-radius: 1 * @toVw ;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
border-radius: 50%; /*外观设置为圆形*/
height: 15 * @toVw;
width: 15 * @toVw;
margin-top: -6 * @toVw; /*使滑块超出轨道部分的偏移量相等*/
background: #ffffff;
border: solid 1* @toVw rgba(205, 224, 230, 0.5); /*设置边框*/
box-shadow: 0 0 2 * @toVw #3b4547; /*添加底部阴影*/
}
.audioOff {
padding: 5 * @toVw;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #888;
background: white;
border-radius: 500px;
}
.ofplayBox {
width: 100%;
height: 30 * @toVw;
margin: 0;
display: flex;
}
.isPlayImg {
width: 40 * @toVw;
height: 100%;
}
.isPlayImg img {
width: 30 * @toVw;
}
.progress {
flex: 1;
position: relative;
font-size: 12px;
}
.duration {
display: flex;
font-size: 12px;
margin-left: 5 * @toVw;
justify-content: space-between;
span:first-child{
margin-left: 0;
}
}
.right-timer{
margin-right: 0.27rem;
}
@media screen and (orientation: landscape) {
.audioOff {
padding: 2 * @toVw;
display: flex;
justify-content: center;
align-items: center;
border: 2px solid #888;
background: white;
border-radius: 500px;
}
input[type=range] {
-webkit-appearance: none;
display: flex;
width: 90%;
padding: 0 5 * @toVw;
border-radius: 10px; /*这个属性设置使填充进度条时的图形为圆角*/
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type=range]::-webkit-slider-runnable-track {
height: 2 * @toVw;
background: #999999;
border-radius: 1 * @toVw ;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
border-radius: 50%; /*外观设置为圆形*/
height: 10 * @toVw;
width: 10 * @toVw;
margin-top: -4 * @toVw; /*使滑块超出轨道部分的偏移量相等*/
background: #ffffff;
border: solid 1* @toVw rgba(205, 224, 230, 0.5); /*设置边框*/
box-shadow: 0 .125em .125em #3b4547; /*添加底部阴影*/
}
.ofplayBox {
width: 100%;
height: 20 * @toVw;
margin: 0;
display: flex;
}
.isPlayImg {
width: 24 * @toVw;
margin-right: 5 * @toVw;
height: 100%;
}
.isPlayImg img {
width: 20 * @toVw;
}
.progress {
flex: 1;
position: relative;
font-size: 12px;
}
.duration {
display: flex;
font-size: 12px;
margin-left: 5 * @toVw;
justify-content: space-between;
span:first-child{
margin-left: 0;
}
}
.right-timer{
margin-right: 0.27rem;
}
}
</style>