Should fix autoplay
This commit is contained in:
@@ -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
|
||||
-------
|
||||
|
||||
81
ytmnd.js
81
ytmnd.js
@@ -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,
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user