Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
2
Indexable
Never
<script>
		var xaudio = document.querySelector("#xaudio");
		var xaudio_id = 0;
		var xaudio_stop = false;
		var xaudio_src = '';
		var xaudio_ct = 0;
		var xaudio_sync = null;

		function xplay(src, target) {
			xaudio_stop = false;
			xaudio_id++;
			let myid = xaudio_id;
			xaudio_src = '.' + src.toString().split('src=')[1].split(' type=')[0].split('course')[1].slice(0, -1);
			xaudio_ct = target.currentTime;
			console.log(xaudio_src);
			setTimeout(() => {
				console.log(`M: ${myid} N: ${xaudio_id}`);
				if (myid == xaudio_id) {
					console.log("play");
					xaudio.src = xaudio_src;
					xaudio.currentTime = xaudio_ct;
					xaudio.load();
					xaudio.play();
					target.play();

					setTimeout(() => {
						xaudio_stop = true;
						if (xaudio_sync != null) {
							clearInterval(xaudio_sync);
							xaudio_sync = null;
							console.log("Cleaning xaudio")
						}
						if (target.duration > 5) {
							xaudio_sync = setInterval(() => {
								if (parseInt(xaudio.currentTime) != parseInt(target.currentTime)) {
									xaudio.currentTime = target.currentTime;
								}
							}, 250);
						} else {
							xaudio_sync = setInterval(() => {
								console.log(xaudio.currentTime);
								if(xaudio.currentTime == 0){
									xaudio.play();
									target.currentTime = 0;
								}
								if (xaudio.currentTime > 0.5 && xaudio.currentTime < 1) {
									target.currentTime = 0;
								}
							}, 250);
						}
					}, 200);
				}
			}, 200)
		}

		xaudio.addEventListener("pause", () => {
			if(xaudio_stop){
				xaudio_stop = false;
				if (xaudio_sync != null) {
					clearInterval(xaudio_sync);
					xaudio_sync = null;
					console.log("Cleaning xaudio")
				}
				console.log("Pausou");
			}
		})

	</script>