4

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");
        }
      },
    }
roshambo
  • 2,624
  • 6
  • 31
  • 54

1 Answers1

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>
roshambo
  • 2,624
  • 6
  • 31
  • 54