修改播放声音通用组件

This commit is contained in:
田岩
2025-08-27 21:04:34 +08:00
parent 42b22ebc95
commit b68783956e
13 changed files with 221 additions and 149 deletions
+21 -4
View File
@@ -102,6 +102,7 @@
type: String
}
});
const audioStore = useAudioStore();
const { dataInfo } = toRefs(props);
const status = computed(() => props.status);
@@ -146,11 +147,15 @@
};
const playVoice = () => {
console.log('播放音频', props.dataInfo.voicePath);
isPlaying.value = true;
audioStore.playAudio(props.dataInfo.voicePath);
// isPlaying.value = true;
console.log('点击播放');
audioStore.playAudio();
};
onMounted(() => {
// 2. 监听 Store 中 isPlaying 的变化,实时同步
const unsubscribe = audioStore.$subscribe((mutation, state) => {
isPlaying.value = state.isPlaying;
});
// 注册播放完成回调
audioStore.onAudioEnded(() => {
console.log('播放完成(通过回调)');
@@ -158,17 +163,29 @@
isPlaying.value = false;
}, 60);
});
// 注册播放错误回调
audioStore.onAudioError((error) => {
console.error('播放错误(通过回调):', error);
});
// 声音加载完毕
audioStore.onAudioLoaded((e) => {
voiceTime.value = (Math.ceil(e.duration) || 1) + 's';
});
watch(
() => props.dataInfo.voicePath,
(newVal) => {
audioStore.setAudioSrc(newVal);
},
{ deep: true, immediate: true }
);
});
onUnload(() => {
// 1. 停止当前播放(优先处理,避免音频继续播放)
audioStore.stopAudio();
// 2. 移除当前组件注册的回调(避免内存泄漏)
audioStore.removeCallbacks();
unsubscribe();
});
</script>
+3 -3
View File
@@ -25,7 +25,7 @@
{{ subject_data.qnsContent }}
</view>
<view :style="props.mode==='mistake'?animationStyle:{}" class="collapse-wrapper" ref="wrapperRef">
<view :style="props.mode === 'mistake' ? animationStyle : {}" class="collapse-wrapper" ref="wrapperRef">
<view class="collapse-content" ref="contentRef">
<view class="option_div">
<template v-if="qnsTyp === 'ES'">
@@ -96,7 +96,7 @@
const emit = defineEmits(['subject_click', 'update:modelValue']);
const props = defineProps({
modelValue: {
type: Array,
type: [Array, Object],
default: () => []
},
item: {
@@ -147,7 +147,7 @@
let answerText = props.item?.my_answer || props.item.anserResult || [];
// item_answer 是答题结果,可以是对象 数组 字符串类型
const item_answer = ref(typeof answerText === 'string' ? answerText.split(',') : answerText);
const feedback_click = () => {
emit('subject_click', 'feedback', props.item);
};