最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to play mp3 file in Vue3 - Stack Overflow

programmeradmin5浏览0评论

below is my code

audio.value?.play();

will cause 'paused on promise rejection' in chrome

<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>

below is my code

audio.value?.play();

will cause 'paused on promise rejection' in chrome

<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>

Share Improve this question asked May 26, 2021 at 10:19 shunze.chenshunze.chen 3593 silver badges10 bronze badges 2
  • answered question : stackoverflow./questions/62824950/… – Escanor Commented May 26, 2021 at 10:30
  • @Escanor Thank you for the ments,but it only work in vuejs2 – shunze.chen Commented May 26, 2021 at 11:05
Add a ment  | 

2 Answers 2

Reset to default 6

I found why it dosen't work. the HTMLDivElement cause the problem. below code will work in Vue3 with ts

<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>

The above is a helpful answer and I voted it up, but I wanted to point out a couple issues. You can't have an audio tag auto-play without a click event from the user. Also, instead of "audio.value.play" it should be "audio.play". Here's an example using a custom play/pause button:

<template>
  <div>
    <button v-on:click="togglePlaying"></button>
    <audio
      hidden="true"
      ref="audio"
    >
    <source src="../../../assets/music/boom.mp3" type="audio/mpeg">
    </audio>
  </div>

</template>
<script lang='ts'>
import { defineComponent, ref } from "vue";
export default defineComponent({
  name: "CommunicateVoice",
  data() {
    return {
      playing: false
    }
  },
  setup() {
    const audio = ref<HTMLAudioElement>();
    return {
      audio,
    };
  },
  methods: {
    toggleAudio() {
      this.playing = !this.playing
      if (this.playing) {
        this.audio.play()
      } else {
        this.audio.pause()
      }
    }
  }
});
</script>
<style scoped>
</style>
发布评论

评论列表(0)

  1. 暂无评论