yz-audio.vue
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
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
<template>
<view class="imt-audio">
<template>
<view class="imt-audio-1">
<view>{{formatSeconds(currentTime)}}</view>
<view class="imt-audio-1-middle">
<slider
class="audio-slider"
activeColor="#fff"
backgroundColor="rgba(255, 255, 255, 0.1)"
block-size="12"
:max="duration"
:value="currentTime"
@change="sliderChange"
@changing="sliderChanging"></slider>
</view>
<view>{{formatSeconds(duration)}}</view>
</view>
<view class="imt-audio-2">
<view class="imt-audio-2-left"></view>
<view class="imt-audio-2-middle">
<!-- <view class="imt-audio-2-middle-left" @click="previousSong">
<image src="@/static/imagesV2/icon49.png"></image>
</view> -->
<view></view>
<view class="imt-audio-2-middle-middle">
<image src="@/static/imagesV2/icon50.png" @click="play" v-if="playState=='pause'"></image>
<image src="@/static/imagesV2/icon51.png" @click="pause" v-else></image>
</view>
<view></view>
<!-- <view class="imt-audio-2-middle-right" @click="nextSong">
<image src="@/static/imagesV2/icon52.png"></image>
</view> -->
</view>
</view>
</template>
</view>
</template>
<script>
export default {
props: {
info: {
type: Object,
default: () => {
return {}
}
},
// 歌曲标题
title: {
type: [String]
},
// 专辑名称
epname: {
type: [String]
},
// 歌手
singer: {
type: [String]
},
autoplay: {
type: [Boolean],
default: false
}
},
data() {
return {
src: '',
duration:0,
currentTime:0,
playState:"pause",//"loading"/"playing"/"pause"
isSliderChanging:false,
backgroundAudioManager: wx.getBackgroundAudioManager()
};
},
mounted: function(){
if(this.autoplay) {
// this.play()
}
},
methods:{
previousSong() {
this.$emit('previousSong')
},
nextSong() {
this.$emit('nextSong')
},
selectItem(item) {
this.$emit('selectItem', item)
this.$refs.NotificationD.show = false
},
setSrc(src) {
this.src = src
this.play()
},
openNotificationD() {
this.$refs.NotificationD.show = true
},
playerOnPlay:function(e)
{
this.playState="playing";
this.duration = this.backgroundAudioManager.duration
this.backgroundAudioManager.onTimeUpdate(() => {
this.currentTime = this.backgroundAudioManager.currentTime
})
this.$emit("play");
},
playerOnPause:function(e)
{
this.playState="pause";
this.$emit("pause");
},
playerOnEnded:function(e)
{
this.playState="pause";
this.$emit("ended");
},
playerOnTimeupdate:function(e)
{
this.playState="playing";
this.duration = e.detail.duration;
this.currentTime = e.detail.currentTime;
this.$emit("timeUpdate",e.detail);
},
playerOnWaiting:function(e)
{
this.playState="loading";
},
playerOnError:function(e)
{
uni.showToast({
title:"播放出错"+e
});
this.playState="pause";
this.$emit("error",e);
},
formatSeconds:function(seconds){
var result = typeof seconds === "string" ? parseFloat(seconds) : seconds;
if (isNaN(result))
return "";
let h = Math.floor(result / 3600) < 10
? "0" + Math.floor(result / 3600)
: Math.floor(result / 3600);
let m = Math.floor((result / 60) % 60) < 10
? "0" + Math.floor((result / 60) % 60)
: Math.floor((result / 60) % 60) + h * 60;
let s = Math.floor(result % 60) < 10
? "0" + Math.floor(result % 60)
: Math.floor(result % 60);
return `${m}:${s}`;
},
play:function(){
this.backgroundAudioManager.title = this.title;//
this.backgroundAudioManager.epname = this.epname;//
this.backgroundAudioManager.singer = this.singer;//
this.backgroundAudioManager.src = this.src
this.backgroundAudioManager.onPlay(this.playerOnPlay)
// this.setProgress(10)
this.backgroundAudioManager.onEnded(this.playerOnEnded)
this.backgroundAudioManager.onError(this.playerOnError)
},
pause:function(){
this.backgroundAudioManager.pause();
this.backgroundAudioManager.onPause(this.playerOnPause)
},
setProgress(num) {
if (num) {
this.backgroundAudioManager.seek(num)
}
},
sliderChange:function(e){
this.isSliderChanging = false;
//要通过e.detail.value获取,否则如果通过dom去读取slider的value
//就会存在滚动条拖不动的情况
this.backgroundAudioManager.seek(e.detail.value)
},
sliderChanging:function()
{
this.isSliderChanging = true;
},
},
}
</script>
<style lang="scss">
@import './index.scss';
</style>