javascript - Trying to play mp3 using Vue.js - Stack Overflow

admin2025-04-19  0

Goal:

  • Trying to add a background sound to my project with a local file that has a play pause state.

Loading an external URL file seems ok and plays/pauses fine, but not local file.

Question:

  • Also what is the exact reason a URL http://....mp3 file plays and my local does not?

Issue:

  • Network status is Status Code: 206 Partial Content
  • Console error is Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

HTML:

    <div class="music-player">
        <audio
          ref="audio"
          preload="auto"
          volume="0.1"
          muted
          loop
        >
          <source :src="file" />
        </audio>
        <div @click="toggleSound(file)" class="toggle-sound paused"></div>
      </div>

JS:

    data: () => ({
      ...,
      file: "/public/audio/bg-music.mp3"
    }),
    methods: {
    toggleSound() {
        let audio = this.$refs.audio;
        if (
          audio.paused &&
          document.querySelector(".toggle-sound").classList.contains("paused")
        ) {
          console.log("play it")
          audio.play();
          document.querySelector(".toggle-sound").classList.remove("paused");
        } else {
          console.log("pause it")
          audio.pause();
          document.querySelector(".toggle-sound").classList.add("paused");
        }
      },
    }

Goal:

  • Trying to add a background sound to my project with a local file that has a play pause state.

Loading an external URL file seems ok and plays/pauses fine, but not local file.

Question:

  • Also what is the exact reason a URL http://....mp3 file plays and my local does not?

Issue:

  • Network status is Status Code: 206 Partial Content
  • Console error is Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

HTML:

    <div class="music-player">
        <audio
          ref="audio"
          preload="auto"
          volume="0.1"
          muted
          loop
        >
          <source :src="file" />
        </audio>
        <div @click="toggleSound(file)" class="toggle-sound paused"></div>
      </div>

JS:

    data: () => ({
      ...,
      file: "/public/audio/bg-music.mp3"
    }),
    methods: {
    toggleSound() {
        let audio = this.$refs.audio;
        if (
          audio.paused &&
          document.querySelector(".toggle-sound").classList.contains("paused")
        ) {
          console.log("play it")
          audio.play();
          document.querySelector(".toggle-sound").classList.remove("paused");
        } else {
          console.log("pause it")
          audio.pause();
          document.querySelector(".toggle-sound").classList.add("paused");
        }
      },
    }
Share Improve this question edited Jul 10, 2020 at 23:36 roshambo asked Jul 9, 2020 at 22:58 roshamboroshambo 2,8048 gold badges33 silver badges57 bronze badges 1
  • stackoverflow./questions/21737224/… – The Alpha Commented Jul 9, 2020 at 23:02
Add a ment  | 

1 Answer 1

Reset to default 2

I simply added the audio src to the audio tag like the below and it worked!

<div class="music-player">
    <audio
      ref="audio"
      src="@/assets/audio/bg-music.ogg"
      preload
      loop
      id="audio"
      muted
    ></audio>
    <div @click="toggleSound()" class="toggle-sound"></div>
  </div>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002053a279288.html

最新回复(0)