Should fix autoplay

This commit is contained in:
2025-12-12 18:10:12 +01:00
parent 6f0866df97
commit 14e417d72a
2 changed files with 42 additions and 41 deletions

View File

@@ -12,7 +12,7 @@ serving
this scraper will download the gif and mp3 from a ytmnd and write a file embedding these things in addition to zoom text (if any).
The downloaded files cannot be loaded from a `file://` url. In order to view these files, put them online or run a local server. For example, `python -m http.server` from the directory and got to [http://localhost:8000/](http://localhost:8000/)
The downloaded files cannot be loaded from a `file://` url. In order to view these files, put them online or run a local server. For example, `python -m http.server` from the directory and got to [http://localhost:8000/](http://localhost:8000/). If you host them somewhere, remember to include `ytmnd.js` in the same directory.
options
-------

View File

@@ -1,42 +1,43 @@
(function () {
var AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var request = new XMLHttpRequest();
var source;
var buffer;
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function () {
context.decodeAudioData(
request.response,
function (response) {
buffer = response;
function playBuffer() {
source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.onended = function () {
playBuffer();
};
source.start(0);
}
playBuffer();
},
function (error) {
console.error("Audio decoding failed:", error);
},
);
};
request.onerror = function () {
console.error("Failed to load audio file");
};
request.send();
var audio = new Audio(url);
audio.loop = true;
audio.muted = true;
audio
.play()
.then(function () {
console.log("Audio started (muted). Click/tap to unmute!");
var unmuteMsg = document.createElement("div");
unmuteMsg.textContent = "Click to unmute";
unmuteMsg.style.cssText =
"position:fixed;top:10px;right:10px;background:rgba(0,0,0,0.8);color:#fff;padding:10px 20px;border-radius:5px;font-family:sans-serif;z-index:9999;cursor:pointer;";
document.body.appendChild(unmuteMsg);
function unmute() {
audio.muted = false;
unmuteMsg.remove();
console.log("Audio unmuted!");
}
document.addEventListener("click", unmute, { once: true });
document.addEventListener("keydown", unmute, { once: true });
document.addEventListener("touchstart", unmute, { once: true });
unmuteMsg.addEventListener("click", unmute, { once: true });
})
.catch(function (error) {
console.error("Autoplay failed even when muted:", error);
function playOnInteraction() {
audio.muted = false;
audio
.play()
.then(function () {
console.log("Audio started after user interaction");
})
.catch(function (err) {
console.error("Still couldn't play:", err);
});
}
document.addEventListener("click", playOnInteraction, { once: true });
document.addEventListener("keydown", playOnInteraction, { once: true });
document.addEventListener("touchstart", playOnInteraction, {
once: true,
});
});
})();