| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <template>
- <div class="audio-wrapper">
- <div v-if="'big' === viewSize" class="audio-big">
- <div v-if="'list' === viewMethod">
- <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ curAudioIndex + 1 }}. {{ fileName }}</div>
- <div v-if="showSlider" class="slider-area">
- <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
- <el-slider
- v-model="play_value"
- class="audio-slider"
- :format-tooltip="formatProcessToolTip"
- :style="{ '--slider-color': topicColor }"
- @change="changeCurrentTime"
- />
- <span class="audio-time">{{ audio_allTime }}</span>
- </div>
- <div class="audio-icon">
- <SvgIcon icon-class="pre" size="12" :color="topicColor" @click="changeFile('prev')" />
- <SvgIcon :icon-class="iconClass" :color="topicColor" size="26" @click="playAudio" />
- <SvgIcon icon-class="next" size="12" :color="topicColor" @click="changeFile('next')" />
- </div>
- </div>
- <div v-else class="independent-big">
- <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
- <div class="slider-area">
- <SvgIcon :icon-class="iconClass" size="18" :color="topicColor" @click="playAudio" style="cursor: pointer" />
- <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
- <el-slider
- v-model="play_value"
- class="audio-slider"
- :format-tooltip="formatProcessToolTip"
- :style="{ '--slider-color': topicColor }"
- @change="changeCurrentTime"
- />
- <span class="audio-time">{{ audio_allTime }}</span>
- </div>
- </div>
- </div>
- <!-- <div v-else-if="'middle' === viewSize" class="audio-middle">
- <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
- <div class="slider-area">
- <SvgIcon :icon-class="iconClass" size="18" :color="topicColor" @click="playAudio" />
- <span class="audio-time">{{ secondFormatConversion(audio.current_time) }}</span>
- <el-slider
- v-model="play_value"
- class="audio-slider"
- :format-tooltip="formatProcessToolTip"
- :style="{ '--slider-color': topicColor }"
- @change="changeCurrentTime"
- />
- <span class="audio-time">{{ audio_allTime }}</span>
- </div>
- </div> -->
- <div v-else-if="'middle' === viewSize" class="audio-middle">
- <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
- <span class="icon-box" :style="{ borderColor: topicColor }">
- <SvgIcon
- :icon-class="iconClass"
- size="12"
- :color="topicColor"
- :style="{ marginLeft: audio.paused ? '4px' : '0' }"
- @click="playAudio"
- />
- </span>
- </div>
- <div v-else-if="'small' === viewSize" class="audio-small">
- <div v-if="fileNameDisplay == 'true'" class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</div>
- <SvgIcon
- icon-class="sound"
- size="20"
- :color="topicColor"
- @click="playAudio"
- :class="{ 'sound-icon--playing': !audio.paused }"
- />
- </div>
- <div v-else :class="['audio-list', { active: audioIndex === curAudioIndex }]" :style="cssVars">
- <div class="active-mark">
- <SvgIcon
- v-if="audioIndex === curAudioIndex"
- icon-class="paused"
- size="12"
- :color="topicColor"
- style="cursor: default"
- />
- </div>
- <span class="audio-name">{{ audioIndex + 1 }}. {{ fileName }}</span>
- <span class="audio-time">{{ audio_allTime }}</span>
- </div>
- <audio
- :id="fileId"
- :ref="fileId"
- :src="url"
- preload="metadata"
- @loadedmetadata="onLoadedmetadata"
- @timeupdate="onTimeupdate"
- @canplaythrough="oncanplaythrough"
- ></audio>
- </div>
- </template>
- <script>
- import { GetFileURLMap } from '@/api/app';
- import { secondFormatConversion } from '@/utils/transform';
- export default {
- name: 'AudioPlay',
- props: {
- fileId: {
- type: String,
- required: true,
- },
- fileName: {
- type: String,
- default: '',
- },
- viewSize: {
- type: String,
- default: 'big',
- },
- viewMethod: {
- type: String,
- default: 'independent',
- },
- audioIndex: {
- type: Number,
- default: 0,
- },
- curAudioIndex: {
- type: Number,
- default: 0,
- },
- showSlider: {
- type: Boolean,
- default: false,
- },
- // 是否显示音频进度条
- showProgress: {
- type: Boolean,
- default: true,
- },
- fileNameDisplay: {
- type: String,
- default: 'true',
- },
- topicColor: {
- type: String,
- default: '#076aff',
- },
- assistColor: {
- type: String,
- default: '#076aff',
- },
- },
- data() {
- return {
- secondFormatConversion,
- url: '',
- audio: {
- paused: true,
- playing: false,
- // 音频当前播放时长
- current_time: 0,
- // 音频最大播放时长
- max_time: 0,
- isPlaying: false,
- loading: false,
- },
- play_value: 0,
- audio_allTime: null, // 展示总时间
- };
- },
- computed: {
- iconClass() {
- return this.audio.paused ? 'paused' : 'playing';
- },
- cssVars() {
- return {
- '--assist-color': this.assistColor,
- };
- },
- },
- watch: {
- fileId: {
- handler(val) {
- if (!val) return;
- GetFileURLMap({ file_id_list: [val] }).then(({ url_map }) => {
- this.url = url_map[val];
- });
- },
- immediate: true,
- },
- },
- mounted() {
- if (!this.fileId) return;
- this.$refs[this.fileId].addEventListener('ended', () => {
- this.audio.paused = true;
- });
- this.$refs[this.fileId].addEventListener('pause', () => {
- this.audio.paused = true;
- });
- this.$refs[this.fileId].addEventListener('play', () => {
- this.audio.paused = false;
- });
- },
- methods: {
- getCurTime(curTime) {
- this.curTime = curTime * 1000;
- },
- playAudio() {
- if (!this.url) return;
- const audio = this.$refs[this.fileId];
- let audioArr = document.getElementsByTagName('audio');
- if (audioArr && audioArr.length > 0) {
- for (let i = 0; i < audioArr.length; i++) {
- if (audioArr[i].src === this.url) {
- if (audioArr[i].id !== this.fileId) {
- audioArr[i].pause();
- }
- } else {
- audioArr[i].pause();
- }
- }
- }
- audio.paused ? audio.play() : audio.pause();
- },
- // 进度条格式化toolTip
- formatProcessToolTip(index) {
- let _index = parseInt((this.audio.max_time / 100) * index);
- return secondFormatConversion(_index);
- },
- // 点击 拖拽播放音频
- changeCurrentTime(value) {
- let audioId = this.fileId;
- this.$refs[audioId].play();
- this.audio.playing = true;
- this.$refs[audioId].currentTime = parseInt((value / 100) * this.audio.max_time);
- },
- // 音频加载完之后
- onLoadedmetadata(res) {
- this.audio.max_time = parseInt(res.target.duration);
- this.audio_allTime = secondFormatConversion(this.audio.max_time);
- },
- // 当音频当前时间改变后,进度条也要改变
- onTimeupdate(res) {
- let audioId = this.fileId;
- this.audio.current_time = res.target.currentTime;
- this.play_value = (this.audio.current_time / this.audio.max_time) * 100;
- if (this.audio.current_time * 1000 > this.ed) {
- this.$refs[audioId].pause();
- }
- },
- onTimeupdateTime(res, playFlag) {
- if (!res && res !== 0) return;
- let audioId = this.audioId;
- this.$refs[audioId].currentTime = res;
- this.play_value = (res / this.audio.max_time) * 100;
- if (playFlag) {
- let audio = document.getElementsByTagName('audio');
- audio.forEach((item) => {
- if (item.id !== audioId) {
- item.pause();
- }
- });
- this.$refs[audioId].play();
- }
- },
- oncanplaythrough() {
- this.audio.loading = false;
- },
- changeFile(type) {
- this.$emit('changeFile', type);
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .audio-wrapper {
- .audio-time {
- white-space: nowrap;
- }
- .audio-big {
- display: flex;
- flex-direction: column;
- padding: 12px 0;
- text-align: center;
- .audio-name {
- padding: 8px;
- margin-bottom: 8px;
- font-size: 14px;
- background-color: #eee;
- border-radius: 4px;
- }
- .audio-icon {
- display: flex;
- column-gap: 32px;
- align-items: center;
- justify-content: center;
- .svg-icon {
- cursor: pointer;
- }
- }
- }
- .independent-big {
- .audio-name {
- text-align: left;
- }
- }
- .audio-middle1 {
- width: 280px;
- padding: 12px 16px;
- border-radius: 4px;
- .audio-name {
- padding: 8px;
- margin-bottom: 8px;
- font-size: 14px;
- text-align: left;
- background-color: #eee;
- border-radius: 4px;
- }
- }
- .audio-middle {
- display: flex;
- flex-direction: column;
- column-gap: 10px;
- align-items: left;
- margin-left: 5px;
- .audio-name {
- padding: 8px;
- margin-bottom: 8px;
- font-size: 14px;
- text-align: center;
- background-color: #eee;
- border-radius: 4px;
- }
- .icon-box {
- display: flex;
- align-items: center;
- justify-content: center;
- width: 32px;
- height: 32px;
- cursor: pointer;
- border: 4px solid #076aff;
- border-radius: 20px;
- // .svg-icon {
- // color: #fff;
- // }
- }
- }
- .audio-small {
- display: flex;
- flex-direction: column;
- column-gap: 10px;
- align-items: left;
- margin-left: 10px;
- cursor: pointer;
- .audio-name {
- padding: 8px;
- margin-bottom: 8px;
- font-size: 14px;
- text-align: center;
- background-color: #eee;
- border-radius: 4px;
- }
- }
- .audio-list {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 8px 24px 8px 16px;
- &.active {
- //background-color: #ebb572;
- background-color: var(--assist-color, #f4f9ff);
- }
- .active-mark {
- width: 20px;
- height: 28px;
- }
- .audio-name {
- flex: 1;
- }
- }
- .slider-area {
- display: flex;
- column-gap: 16px;
- align-items: center;
- justify-content: space-between;
- font-size: 14px;
- .audio-slider {
- width: 90%;
- :deep .el-slider__runway {
- height: 4px;
- }
- :deep .el-slider__button {
- width: 4px;
- height: 4px;
- margin-top: -1px;
- border: none;
- }
- :deep .el-slider__bar {
- height: 4px;
- background-color: var(--slider-color, #076aff);
- }
- }
- }
- }
- .sound-icon--playing {
- animation: soundWave 1s infinite;
- }
- @keyframes soundWave {
- 0%,
- 100% {
- opacity: 1;
- transform: scale(1);
- }
- 50% {
- opacity: 0.5;
- transform: scale(1.2);
- }
- }
- </style>
|