Created
March 23, 2019 00:35
-
-
Save fxp/8ae7526cbdd30709b8cdbfbadf68f74b to your computer and use it in GitHub Desktop.
Media audio availability test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<!--<script src="https://unpkg.com/wavesurfer.js"></script>--> | |
</head> | |
<body> | |
<video id="video" | |
crossorigin="anonymous" | |
src="https://fxp-1258505122.cos.ap-chengdu.myqcloud.com/73ec045e66dc8f6a3ad8049deec08c40_norm.mp4"></video> | |
<div id="waveform"></div> | |
<button data-playing="false" role="switch" aria-checked="false" onclick="play()"> | |
<span>Play/Pause</span> | |
</button> | |
<script> | |
var audioContext = undefined; | |
let analyser = undefined; | |
let mediaElt = undefined; | |
function initAudioContext() { | |
audioContext = new (window.AudioContext || window.webkitAudioContext)(); | |
analyser = audioContext.createAnalyser(); | |
audioContext.crossOrigin = "anonymous"; | |
mediaElt = document.querySelector('video'); | |
let source = audioContext.createMediaElementSource(mediaElt); | |
source.connect(analyser); | |
} | |
function play() { | |
if (!audioContext) { | |
initAudioContext(); | |
} | |
// track.connect(audioContext.destination); | |
mediaElt.play(); | |
let badAudio = true; | |
let checker = setInterval(() => { | |
let dataArray = new Uint8Array(analyser.fftSize); | |
analyser.getByteTimeDomainData(dataArray); | |
for (let d of dataArray) { | |
if (d != 128) { | |
badAudio = false | |
clearInterval(checker); | |
clearTimeout(timeoutChecker); | |
break; | |
} | |
} | |
}, 100) | |
function onVideoEnd() { | |
console.log('result1', badAudio); | |
clearInterval(checker); | |
clearTimeout(timeoutChecker); | |
mediaElt.removeEventListener('ended', onVideoEnd); | |
} | |
mediaElt.addEventListener('ended', onVideoEnd) | |
let timeoutChecker = setTimeout(_ => { | |
console.log('result2', badAudio); | |
mediaElt.pause(); | |
clearInterval(checker); | |
clearTimeout(timeoutChecker); | |
mediaElt.removeEventListener('ended', onVideoEnd); | |
}, 8000); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment