目标:
音频在第一次播放/暂停切换时淡入淡出,需要让它不断切换声音
问题
第二次切换音频剪切和跳过
HTML
<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>
JS -更新
methods: {
toggleSound() {
let backgroundAudio = this.$refs.audio;
//Fade In
if (
!document.querySelector(".toggle-sound").classList.contains("paused")
) {
console.log("fading in");
let actualVolumeFadeIn = 0;
document.querySelector(".toggle-sound").classList.add("paused");
clearInterval(fadeInInterval);
let fadeInInterval = setInterval(function() {
actualVolumeFadeIn = (parseFloat(actualVolumeFadeIn) + 0.1).toFixed(
1
);
if (actualVolumeFadeIn <= 1) {
backgroundAudio.volume = actualVolumeFadeIn;
} else {
backgroundAudio.play();
}
}, 100);
return false;
}
//Fade Out
if (
document.querySelector(".toggle-sound").classList.contains("paused")
) {
console.log("fading out");
let actualVolumeFadeOut = backgroundAudio.volume;
clearInterval(fadeOutInterval);
let fadeOutInterval = setInterval(function() {
actualVolumeFadeOut = (parseFloat(actualVolumeFadeOut) - 0.1).toFixed(
1
);
if (actualVolumeFadeOut >= 0) {
backgroundAudio.volume = actualVolumeFadeOut;
} else {
document.querySelector(".toggle-sound").classList.remove("paused");
backgroundAudio.pause();
}
}, 100);
return false;
}
},
}
qq_花开花谢_0
相关分类