When I click a button, I am able to get the correct value of AudioTracks.length. However, without clicking the button, I am unable to retrieve this value. Take a look at my code below. It works at line (B) but not at (A). Can you help me understand why and how I can make it work at (A)? I have a feeling that there's some basic concept I'm missing here, so any assistance would be greatly appreciated. Thank you!
[My code]
<!DOCTYPE html>
<html>
<body>
<h3>Please use a browser supporting AudioTracks property. See [Note] for details.</h3>
<video id="video" controls>
<source src="https://www.w3schools.com/tags/mov_bbb.mp4"></source >
</video>
<button onclick="myFunction()">Click (myFunction)</button>
<script>
console.log("(A) = " + video.audioTracks.length );// (A) = 0
function myFunction() {
console.log("(B) = " + video.audioTracks.length );// (B) = 2
}
</script>
</body>
</html>
[Note] The audioTracks property is not supported in any major browsers. It is supported in Chrome beta by enabling "enable-experimental-web-platform-features" in chrome:flags.
- Download Chrome beta Version 107.
- Type "chrome://flags/" in the address bar to see "Experiments" page.
- Enable "#enable-experimental-web-platform-features".
[Reference] https://caniuse.com/audiotracks