fuliqi
2024-08-15 02b9931c5e3a54d69d4d20a9dc6aad141fc35141
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
<template>
  <div class="audio-container">
    <div class="audio_wrap_content">
      <audio ref="audioRef" @play="playFunc" @pause="pauseFunc" @timeupdate="timeupdateFunc"
        @loadedmetadata="onLoadedmetadata" @ended="handleEnd">
        <source :src="audioSrc" />
      </audio>
      <div class="cudio_control_content">
        <el-icon :size="32" color="#3680fa" @click="startPlayOrPause" class="cursor-pointer">
          <VideoPlay v-show="!audio.playing" />
          <VideoPause v-show="audio.playing" />
        </el-icon>
        <div class="slider">
          <span>{{ formattedCurrentTime }}</span>
          <div @mousedown="audio.dragState = true" @mouseup="audio.dragState = false"
            @mouseleave="audio.dragState = false"><el-slider v-model="sliderTime" size="small" :max="audio.maxTime"
              :show-tooltip="false" @change="onChange"></el-slider></div>
          <span>{{ formattedMaxTime }}</span>
        </div>
      </div>
    </div>
 
  </div>
</template>
 
<script setup>
import { ref, computed, onBeforeUnmount } from 'vue';
import { VideoPlay, VideoPause } from '@element-plus/icons-vue';
 
const props = defineProps({
  audioSrc: {
    type: String,
    required: true,
    default: ''
  }
});
const sliderTime = ref(0);
const audioRef = ref(null);
 
const audio = ref({
  maxTime: 0,
  currentTime: 0,
  playing: false,
  dragState: false
});
 
 
const formatTime = (second) => {
  let m = parseInt(second / 60);
  let s = parseInt(second % 60);
  let time = "";
  if (second == 0) {
    return "0'00''";
  }
  if (m == 0) {
    if (s >= 10) {
      time = "0'" + s + "''";
    } else {
      time = "0'0" + s + "''";
    }
  } else {
    if (s >= 10) {
      time = m + "'" + s + "''";
    } else {
      time = m + "'0" + s + "''";
    }
  }
  return time;
};
 
const formattedCurrentTime = computed(() => {
  return formatTime(audio.value.currentTime);
});
const formattedMaxTime = computed(() => {
  return formatTime(audio.value.maxTime);
});
 
const play = () => {
  audioRef.value.play();
};
const pause = () => {
  audioRef.value.pause();
};
const playFunc = () => {
  audio.value.playing = true;
};
const pauseFunc = () => {
  audio.value.playing = false;
};
const handleEnd = () => {
  sliderTime.value = 0;
  audio.value.playing = false;
  audio.value.currentTime = 0;
};
const timeupdateFunc = (res) => {
  if (!audio.value.dragState) {
    audio.value.currentTime = res.target.currentTime;
    sliderTime.value = res.target.currentTime;
  }
};
const onLoadedmetadata = (res) => {
  audio.value.maxTime = parseInt(res.target.duration);
};
const startPlayOrPause = () => {
  audio.value.playing ? pause() : play();
};
const onChange = (value) => {
  audioRef.value.currentTime = value;
};
 
onBeforeUnmount(() => {
  pause();
});
 
</script>
 
<style lang="scss" scoped>
.audio-container {
  width: 400px;
  border: 1px solid #3680fa;
  height: 50px;
  border-radius: 50px;
}
 
.audio_wrap_content {
  height: 100%;
}
 
.cudio_control_content {
  margin: 0 auto;
  width: 90%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
 
  .slider {
    flex: 1;
    width: 100%;
    display: flex;
    align-items: center;
 
  }
 
  .slider div {
    flex: 1;
  }
 
  .slider span {
    margin: 0 15px;
    font-size: 12px;
    color: rgba(34, 34, 34, 0.5);
  }
 
 
}
</style>