summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/media-has-audio.ts
blob: 3421a38a7655504922e65da3bfcc2b6f53c8bb86 (plain)
1
2
3
4
5
6
7
8
9
export default async function hasAudio(media: HTMLMediaElement) {
	const cloned = media.cloneNode() as HTMLMediaElement;
	cloned.muted = (cloned as typeof cloned & Partial<HTMLVideoElement>).playsInline = true;
	cloned.play();
	await new Promise((resolve) => cloned.addEventListener('playing', resolve));
	const result = !!(cloned as any).audioTracks?.length || (cloned as any).mozHasAudio || !!(cloned as any).webkitAudioDecodedByteCount;
	cloned.remove();
	return result;
}