import { defineComponent, reactive, ref, nextTick, onMounted, watch } from 'vue'; import styles from './audio.module.less'; import iconplay from '../image/icon-pause.png'; import iconpause from '../image/icon-play.png'; import iconReplay from '../image/icon-replay.png'; import { NSlider } from 'naive-ui'; import Vudio from 'vudio.js'; import tickMp3 from '../image/tick.mp3'; export default defineComponent({ name: 'audio-play', props: { item: { type: Object, default: () => { return {}; } }, activeStatus: { type: Boolean, default: false }, isEmtry: { type: Boolean, default: false }, imagePos: { type: String, default: 'left' } }, emits: ['loadedmetadata', 'togglePlay', 'ended', 'reset'], setup(props, { emit, expose }) { const audioForms = reactive({ paused: true, currentTimeNum: 0, currentTime: '00:00', durationNum: 0, duration: '00:00', showBar: true, afterMa3: true, count: 0, previousBytesLoaded: 0, previousTime: Date.now() }); const canvas: any = ref(); const audio: any = ref(); let vudio: any = null; // 切换音频播放 const onToggleAudio = (e?: any) => { e?.stopPropagation(); // console.log(audio.value.paused, 'audio.value.paused'); if (audio.value.paused) { audio.value.play(); audioForms.afterMa3 = false; } else { audio.value?.pause(); } audioForms.paused = audio.value?.paused; e?.target?.focus(); emit('togglePlay', audioForms.paused); }; const onInit = (audio: undefined, canvas: undefined) => { if (!vudio) { vudio = new Vudio(audio, canvas, { effect: 'waveform', accuracy: 256, width: 1024, height: 600, waveform: { maxHeight: 200, color: [ [0, '#44D1FF'], [0.5, '#44D1FF'], [0.5, '#198CFE'], [1, '#198CFE'] ], prettify: false } }); vudio.dance(); } }; // 对时间进行格式化 const timeFormat = (num: number) => { if (num > 0) { const m = Math.floor(num / 60); const s = num % 60; return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s); } else { return '00:00'; } }; // const toggleHideControl = (isShow: false) => { audioForms.showBar = isShow; }; const onReplay = () => { if (!audio.value) return; audio.value.currentTime = 0; }; let vudio1 = null; const canvas1: any = ref(); const audio1: any = ref(); nextTick(() => { vudio1 = new Vudio(audio1.value, canvas1.value, { effect: 'waveform', accuracy: 256, width: 1024, height: 600, waveform: { maxHeight: 200, color: [ [0, '#44D1FF'], [0.5, '#44D1FF'], [0.5, '#198CFE'], [1, '#198CFE'] ], prettify: false } }); vudio1.dance(); }); watch( () => props.activeStatus, (val: any) => { // console.log(val, 'val'); audioForms.count = 0; if (val && props.item.autoPlay) { vudio = null; onToggleAudio(); } else { audio.value.pause(); } } ); // onMounted(() => { // console.log(props.item, 'eeeee'); // }); expose({ toggleHideControl }); return () => (
{audioForms.afterMa3 && (
)}
{ e.stopPropagation(); emit('reset'); }}>
{ audio.value.currentTime = val; audioForms.currentTimeNum = val; audioForms.currentTime = timeFormat(Math.round(val || 0)); }} />
{props.imagePos === 'right' ? ( <>
{audioForms.currentTime}
/
{audioForms.duration}
{audioForms.paused ? ( ) : ( )}
37.8M/s
) : ( <>
{audioForms.paused ? ( ) : ( )}
37.8M/s
{audioForms.currentTime}
/
{audioForms.duration}
)}
); } });