background-audio.vue
4.19 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
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap">
<view class="uni-center">
<text class="time-big">{{formatedPlayTime}}</text>
</view>
<view class="uni-common-mt">
<slider class="slider" min="0" max="21" step="1" :value="playTime" @change="seek"></slider>
</view>
<view class="play-time">
<text>00:00</text>
<text>00:21</text>
</view>
<view class="uni-hello-text">注意:离开当前页面后背景音乐将保持播放,但退出uni-app将停止</view>
<view class="page-body-buttons">
<block v-if="playing">
<view class="page-body-button" @tap="stop">
<image src="/static/stop.png"></image>
</view>
<view class="page-body-button" @tap="pause">
<image src="/static/pause.png"></image>
</view>
</block>
<block v-if="!playing">
<view class="page-body-button"></view>
<view class="page-body-button" @tap="play">
<image src="/static/play.png"></image>
</view>
</block>
<view class="page-body-button"></view>
</view>
</view>
</view>
</template>
<script>
import * as util from '../../../common/util.js';
export default {
data() {
return {
title: 'backgroundAudio',
bgAudioMannager: null,
dataUrl: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3',
playing: false,
playTime: 0,
formatedPlayTime: '00:00:00'
}
},
onLoad: function () {
this.playing = this.$backgroundAudioData.playing;
this.playTime = this.$backgroundAudioData.playTime;
this.formatedPlayTime = this.$backgroundAudioData.formatedPlayTime;
let bgAudioMannager = uni.getBackgroundAudioManager();
if(!bgAudioMannager.title){
bgAudioMannager.title = '致爱丽丝';
}
if(!bgAudioMannager.singer) {
bgAudioMannager.singer = '暂无';
}
if(!bgAudioMannager.coverImgUrl){
bgAudioMannager.coverImgUrl = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/c517b410-5184-11eb-b997-9918a5dda011.jpeg';
}
bgAudioMannager.onPlay(() => {
console.log("开始播放");
this.$backgroundAudioData.playing = this.playing = true;
})
bgAudioMannager.onPause(() => {
console.log("暂停播放");
this.$backgroundAudioData.playing = this.playing = false;
})
bgAudioMannager.onEnded(() => {
this.playing = false;
this.$backgroundAudioData.playing = false;
this.$backgroundAudioData.playTime = this.playTime = 0;
this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(0);
})
bgAudioMannager.onTimeUpdate((e) => {
if (Math.floor(bgAudioMannager.currentTime) > Math.floor(this.playTime)) {
this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(Math.floor(bgAudioMannager.currentTime));
}
this.$backgroundAudioData.playTime = this.playTime = bgAudioMannager.currentTime;
})
this.bgAudioMannager = bgAudioMannager;
},
methods: {
play: function (res) {
if (!this.bgAudioMannager.src) {
this.bgAudioMannager.startTime = this.playTime;
this.bgAudioMannager.src = this.dataUrl;
} else {
this.bgAudioMannager.seek(this.playTime);
this.bgAudioMannager.play();
}
},
seek: function (e) {
this.bgAudioMannager.seek(e.detail.value);
},
pause: function () {
this.bgAudioMannager.pause();
},
stop: function () {
this.bgAudioMannager.stop();
this.$backgroundAudioData.playing = this.playing = false;
this.$backgroundAudioData.playTime = this.playTime = 0;
this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(0);
}
}
}
</script>
<style>
image {
width: 150rpx;
height: 150rpx;
}
.page-body-text {
padding: 0 30rpx;
}
.page-body-wrapper {
margin-top: 0;
}
.page-body-info {
padding-bottom: 50rpx;
}
.time-big {
font-size: 60rpx;
margin: 20rpx;
}
.slider {
width:630rpx;
}
.play-time {
font-size: 28rpx;
width:100%;
padding: 20rpx 0;
display: flex;
justify-content: space-between;
box-sizing: border-box;
}
.page-body-buttons {
display: flex;
justify-content: space-around;
margin-top: 100rpx;
}
.page-body-button {
width: 250rpx;
text-align: center;
}
</style>