gpt4 book ai didi

javascript - 如何在Vue3中播放mp3文件

转载 作者:行者123 更新时间:2023-12-05 01:59:36 52 4
gpt4 key购买 nike

下面是我的代码

audio.value?.play();

将在 chrome 中导致“暂停 promise 拒绝”

<template>
<div>
<audio
hidden="true"
ref="audio"
src="../../../assets/music/boom.mp3"
>
</audio>
</div>

</template>
<script lang='ts'>
import { defineComponent, onMounted, ref } from "vue";
export default defineComponent({
name: "CommunicateVoice",
setup() {
const audio = ref<HTMLDivElement>();
onMounted(() => {
audio.value?.play();
});

return {
audio,
};
},
});
</script>

最佳答案

我找到了它不起作用的原因。HTMLDivElement 导致了问题。下面的代码将在 Vue 3 中使用 tsp>

<template>
<div>
<audio
hidden="true"
ref="audio"
>
<source src="../../../assets/music/boom.mp3" type="audio/mpeg">
</audio>
</div>

</template>
<script lang='ts'>
import { defineComponent, onMounted, ref } from "vue";
export default defineComponent({
name: "CommunicateVoice",
setup() {
const audio = ref<HTMLAudioElement>();
onMounted(() => {
console.log(audio);
//@ts-ignore
audio.value?.play()
});

return {
audio,
};
},
});
</script>
<style scoped>
</style>
工作

关于javascript - 如何在Vue3中播放mp3文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67702963/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com