zxl
2025-05-23 5d5b0f7ab0f34019e11901ddcd59cd8b51ea9ff9
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
<template>
  <div class="audio-message">
    <div class="videodisc">
      <div class="disc" :class="{ play: isPlay }" @click="toPlay">
        <i v-if="loading" class="el-icon-loading" />
        <i v-else-if="isPlay" class="el-icon-video-pause" />
        <i v-else class="el-icon-video-play" />
        <audio
          ref="audio"
          type="audio/mp3"
          :src="src"
          @timeupdate="timeupdate"
          @ended="ended"
          @canplay="canplay"
        ></audio>
      </div>
    </div>
    <div class="detail">
      <div class="text">
        <i class="el-icon-service" />
        <span>{{ getCurrDuration }} / {{ getTotalDuration }}</span>
      </div>
      <div class="process">
        <el-progress :percentage="progress" :show-text="false" />
      </div>
    </div>
  </div>
</template>
 
<script>
function formatSeconds(value) {
  var theTime = parseInt(value) // 秒
  var theTime1 = 0 // 分
  var theTime2 = 0 // 小时
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60)
    theTime = parseInt(theTime % 60)
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60)
      theTime1 = parseInt(theTime1 % 60)
    }
  }
 
  var result = '' + parseInt(theTime) //秒
  if (10 > theTime > 0) {
    result = '0' + parseInt(theTime) //秒
  } else {
    result = '' + parseInt(theTime) //秒
  }
 
  if (10 > theTime1 > 0) {
    result = '0' + parseInt(theTime1) + ':' + result //分,不足两位数,首位补充0,
  } else {
    result = '' + parseInt(theTime1) + ':' + result //分
  }
  if (theTime2 > 0) {
    result = '' + parseInt(theTime2) + ':' + result //时
  }
  return result
}
 
export default {
  name: 'AudioMessage',
  props: {
    src: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      loading: true,
      isPlay: false,
      duration: 0,
      currentTime: 0,
      progress: 0,
    }
  },
  computed: {
    getTotalDuration() {
      return formatSeconds(this.duration)
    },
    getCurrDuration() {
      return formatSeconds(this.currentTime)
    },
  },
  methods: {
    toPlay() {
      if (this.loading) {
        return
      }
 
      let audio = this.$refs.audio
      if (this.isPlay) {
        audio.pause()
      } else {
        audio.play()
      }
      this.isPlay = !this.isPlay
    },
 
    // 当目前的播放位置已更改时
    timeupdate() {
      let audio = this.$refs.audio
      this.currentTime = audio.currentTime
      this.progress = (audio.currentTime / audio.duration) * 100
    },
 
    // 当浏览器可以播放音频/视频时
    canplay() {
      this.duration = this.$refs.audio.duration
      this.loading = false
    },
 
    // 当目前的播放列表已结束时
    ended() {
      this.isPlay = false
    },
  },
}
</script>
<style scoped lang="less">
.audio-message {
  width: 200px;
  height: 60px;
  border-radius: 5px;
  background: #ffffff;
  display: flex;
  align-items: center;
  border: 1px solid #03a9f4;
  overflow: hidden;
 
  > div {
    height: 100%;
  }
 
  .videodisc {
    flex-basis: 60px;
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .disc {
      width: 42px;
      height: 42px;
      background: #e9e5e5;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      cursor: pointer;
      transition: ease 0.5;
 
      &.play {
        background: #ff5722;
        box-shadow: 0 0 4px 0px #f76a3e;
      }
 
      i {
        font-size: 24px;
      }
 
      &:active i {
        transform: scale(1.1);
      }
    }
  }
 
  .detail {
    flex: 1 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-top: 10px;
 
    .text {
      width: 90%;
      font-size: 12px;
      i {
        margin-right: 5px;
      }
    }
 
    .process {
      padding-top: 10px;
      height: 20px;
      width: 90%;
    }
  }
}
</style>