<!-- 音乐播放器 -->
<div class="music">
<!--音乐开始controls-->
<audio id="audio" :src="require('../music/击鼓传花.wav')"></audio>
<!--音乐结束-->
<span @click="stopMusic()" v-if="stopBackgroundMusic">
<img src="@/assets/静音02.png"
/></span>
<span @click="playMusic()" v-if="!stopBackgroundMusic"
><img src="@/assets/静音01.png"
/></span>
</div>
HTML
// 播放音乐
const audioAutoPlay = () => {
let audio = document.getElementById("audio");
audio.play();
document.removeEventListener("touchstart", audioAutoPlay);
};
const musicNow = () => {
// 播放音乐
let oAudio = document.querySelector("#audio");
oAudio.onended = function () {
//播放完毕,重新循环播放
oAudio.load();
oAudio.play();
};
audioAutoPlay();
};
// 停止播放音乐
const stopMusic = () => {
let oAudio = document.querySelector("#audio");
oAudio.pause();
state.stopBackgroundMusic = false;
console.log("停止播放");
};
// 继续播放音乐
const playMusic = () => {
musicNow();
console.log("继续播放");
state.stopBackgroundMusic = true;
};
JS
= =网上找到不知道为什么总有莫名其妙的报错,这是改出来好用的