|
|
@@ -52,6 +52,7 @@
|
|
|
:autoplay="autoplay"
|
|
|
:loop="loop"
|
|
|
@timeupdate="onTimeupdate"
|
|
|
+ @canplay="onCanplay"
|
|
|
@ended="audioEnd"
|
|
|
:muted="hanMuted"
|
|
|
>
|
|
|
@@ -104,7 +105,7 @@ export default create({
|
|
|
}
|
|
|
},
|
|
|
components: {},
|
|
|
- emits: ['fastBack', 'play', 'forward', 'ended', 'changeProgress', 'mute'],
|
|
|
+ emits: ['fastBack', 'play', 'forward', 'ended', 'changeProgress', 'mute', 'can-play'],
|
|
|
|
|
|
setup(props, { emit }) {
|
|
|
const audioRef = ref(null);
|
|
|
@@ -144,20 +145,23 @@ export default create({
|
|
|
} catch (e) {
|
|
|
console.log((e as any).message);
|
|
|
}
|
|
|
+ });
|
|
|
|
|
|
- // 获取当前音频播放时长
|
|
|
- setTimeout(() => {
|
|
|
- const audioR = audioRef.value as any;
|
|
|
- // 自动播放
|
|
|
- if (props.autoplay) {
|
|
|
- if (audioR && audioR.paused) {
|
|
|
- audioR.play();
|
|
|
- }
|
|
|
+ // audio canplay 事件触发时
|
|
|
+ const onCanplay = (e: Event) => {
|
|
|
+ const audioR = audioRef.value as any;
|
|
|
+ // 自动播放
|
|
|
+ if (props.autoplay) {
|
|
|
+ if (audioR && audioR.paused) {
|
|
|
+ audioR.play();
|
|
|
}
|
|
|
- audioData.second = audioR.duration;
|
|
|
- audioData.duration = formatSeconds(audioR.duration);
|
|
|
- }, 500);
|
|
|
- });
|
|
|
+ }
|
|
|
+ // 获取当前音频播放时长
|
|
|
+ audioData.second = audioR.duration;
|
|
|
+ audioData.duration = formatSeconds(audioR.duration);
|
|
|
+
|
|
|
+ emit('can-play', e);
|
|
|
+ };
|
|
|
|
|
|
//播放时间
|
|
|
const onTimeupdate = (e: any) => {
|
|
|
@@ -166,7 +170,9 @@ export default create({
|
|
|
|
|
|
//后退
|
|
|
const fastBack = () => {
|
|
|
- audioData.currentTime--;
|
|
|
+ if (audioData.currentTime > 0) {
|
|
|
+ audioData.currentTime--;
|
|
|
+ }
|
|
|
(audioRef.value as any).currentTime = audioData.currentTime;
|
|
|
|
|
|
emit('fastBack', audioData.currentTime);
|
|
|
@@ -265,7 +271,8 @@ export default create({
|
|
|
progressChange,
|
|
|
audioEnd,
|
|
|
onTimeupdate,
|
|
|
- handleMute
|
|
|
+ handleMute,
|
|
|
+ onCanplay
|
|
|
};
|
|
|
}
|
|
|
});
|