1 回答
TA贡献1835条经验 获得超7个赞
我设法让它工作:
HTML
<audio
ref="audio"
src="@/assets/audio/bg-music.ogg"
preload
loop
id="audio"
muted
></audio>
<div
@click="toggleSound()"
class="toggle-sound"
v-bind:class="this.state.backgroundAudioState"
></div>
JS
data: () => ({
...
state: {
backgroundAudioState: "paused",
},
}),
methods: {
toggleSound() {
let backgroundAudio = this.$refs.audio;
let actualVolumeFadeOut = backgroundAudio.volume;
let actualVolumeFadeIn = 0;
//Fade In
if (this.state.backgroundAudioState === "paused") {
console.log("fading in");
this.state.backgroundAudioState = "playing";
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 (this.state.backgroundAudioState === "playing") {
console.log("fading out");
this.state.backgroundAudioState = "paused";
let fadeOutInterval = setInterval(function() {
actualVolumeFadeOut = (parseFloat(actualVolumeFadeOut) - 0.1).toFixed(1);
if (actualVolumeFadeOut >= 0) {
backgroundAudio.volume = actualVolumeFadeOut;
} else {
backgroundAudio.pause();
clearInterval(fadeOutInterval);
}
}, 100);
return false;
}
},
}
添加回答
举报