如何在javascript中做音频暂停和恢复



我怎么能暂停音频和恢复回到它暂停的地方,如果点击,请我需要这个代码的解决方案。

html:

<div class="PlaySong">playPause</div>

Js:

$(".PlaySong").on("click", function () { 
let audioCtx = new AudioContext();
let song = new Audio();
if (audioCtx.state === "running") {
audioCtx.suspend().then(function () {
sb.audioCtx.pause();
$(".jp-play").html(`<i class='ms_play_control'></i>`);
//  susresBtn.textContent = "Resume context";
});
} else {
audioCtx.resume().then(function () {
console.log("play")
song.src = "Whatsapp-Message-Sent-Sound.mp3"; //set the source of 0th song
song.play();
});
}
});

尝试将变量放在处理程序之外

let audioCtx = new AudioContext();
let song = new Audio();
$(".PlaySong").on("click", function () { 
if (audioCtx.state === "running") {
audioCtx.suspend().then(function () {
sb.audioCtx.pause();
$(".jp-play").html(`<i class='ms_play_control'></i>`);
//  susresBtn.textContent = "Resume context";
});
} else {
audioCtx.resume().then(function () {
console.log("play")
song.src = "Whatsapp-Message-Sent-Sound.mp3"; //set the source of 0th song
song.play();
});
}
});

相关内容

  • 没有找到相关文章

最新更新