/    Sign up×
Community /Pin to ProfileBookmark

Rules for html5 audio / video tags questioned

Recently I began to overhaul some of my pages that formerly relied on some flash players for audio and video to make use of html5. My flash player(s) rely on javascript calls that basically replace the contents of a ‘div’ element with a certain ‘id’, and I wanted to keep that as my ‘fallback to flash’ methodology. This meant detecting whether or not the browser supported video or audio, which seemed easy enough, but it was not. And while I did find an approach that worked, I still don’t know why my first method failed and I’d really like to. Consider this html5 audio tag structure…

[CODE]<div id=”musicDiv” align=”center” >

<audio controls autoplay >

<source src=”songSample.mp3″ type=’audio/mpeg; codecs=”mp3″‘>
<source src=”songSample.ogg” type=’audio/ogg; codecs=”vorbis”‘>

<div id=”testHtml5″>
<em>your browser doesn’t support<br>HTML5 audio </em>
</div>

</audio>[/CODE]

Seems pretty standard right? It worked as expected and The “browser doesn’t support” message only shows up in older browsers like IE-8. So next, I placed this javascript in the page..

[CODE]<script language=”JavaScript”>

if (document.getElementById(“testHtml5”) != null)
playUsingFlashPlayer(“songSample.mp3”, musicDiv);
</script>[/CODE]

Now just assume that my playUsingFlashPlayer() call does what its name suggests. Indeed, when I tested this on older browsers, the call would be triggered and the flash player would take over the ‘div’ tag whose id=”musicDiv”, and all worked as planned. But then I tried it on my other browsers like Firefox and Chrome, and now they too attempted to run the flash player.

OK, I did find a workaround which I’ll get to in a minute. For now, I want to understand WHY this failed. I understood that the rules of an html5 ‘audio’ tag said that html included within the tags would only be seen by browsers that didn’t support them. But if that were true, then the ‘div’ with id=”testHtml5″ should NOT have been seen, the getElementById() should have failed, and my flash player call should not have been triggered. WHY DIDN’T IT WORK THAT WAY!!

Incidentally, my workaround is temporary and incomplete, but for now works. I basically replace the javascript call with this…

[CODE]<script language=”JavaScript”>

// helper to decide whether flash is needed
var test_audio= document.createElement(“audio”) //try and create sample audio element
var test_video= document.createElement(“video”) //try and create sample video element
var mediasupport={audio: (test_audio.play)? true : false, video: (test_video.play)? true : false}

if (!mediasupport.audio)
playUsingFlashPlayer(“mp3/cd/DebsMoveSample.mp3”, “yes”);

</script>
[/CODE]

I don’t really like this because first, it really is only determining whether html5 audio is possible, not whether it actually worked, and second, I’m sure somebody’s old browser will confuse the visitor about a javascript “object not found ” error or something. It would be WAY better if the ‘audio’ tag worked the way all the tutorials SAY it will work, and apparently that’s not the case. Anyone know why?

to post a comment
HTML

4 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Feb 26.2015 — I want to understand WHY this failed. I understood that the rules of an html5 'audio' tag said that html included within the tags would only be seen by browsers that didn't support them. But if that were true, then the 'div' with id="testHtml5" should NOT have been seen, the getElementById() should have failed, and my flash player call should not have been triggered. WHY DIDN'T IT WORK THAT WAY!![/QUOTE]

Put this on your page and test it:
&lt;script&gt;
alert(document.getElementById("testHtml5"));
alert(document.getElementById("humptyDumpty"));
&lt;/script&gt;


Notice which alert returns "null". ?

You see, <div id="testHtml5"> is always there. It is never NOT in the code, therefore it's never "null". Browsers which understand the <audio> tag have built in behavior to not display text inside that tagset. Because it's built in and not in the page's stylesheet you can't even test for [B]visibility=[/B] or [B]display=[/B] with JavaScript.

Rather than using a script which may or may not get run, and assuming you are mostly concerned with old versions of IE, you could use an IE Conditional Comment like this:
&lt;!--[if lt IE 9]&gt;
your Flash player code here
&lt;![endif]--&gt;


Put that right below the </audio> tag, leave out any text in the <audio> tag and IE8- will collapse that tagset (basically an empty <div>) and display the Flash player.
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 26.2015 — Put this on your page and test it:

Browsers which understand the <audio> tag have built in behavior to not display text inside that tagset. Because it's built in and not in the page's stylesheet you can't even test for [B]visibility=[/B] or [B]display=[/B] with JavaScript.
[/QUOTE]


Much appreciated Kevin. At least it makes sense now. I suppose this is the same reason why I couldn't put a javascript right in the 'audio' tag, as it too was 'always there' and always executed.

I don't think just detecting an older version of IE is the best approach. Someone could just as well visit with another browser that's new enough to support 'audio', but does not have MP3 or OGG support. If its possible I'd rather actually know if the 'audio' actually worked and the song is actually playing. It does seem very odd that the browser can choose whether to display the HTML based on whether the audio tag played, and yet there is no way to detect it. Its almost like designing an automated electric lock and not bothering to make an override key, thinking nothing could possibly go wrong with the power. :-)
Copy linkTweet thisAlerts:
@Kevin2Feb 26.2015 — Someone could just as well visit with another browser that's new enough to support 'audio', but does not have MP3 or OGG support.[/QUOTE]

Which "browser that's new enough to support 'audio'" does not support at least one of those?
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 26.2015 — Which "browser that's new enough to support 'audio'" does not support at least one of those?[/QUOTE]

I dunno! :-) There are those ACC (mp4, m4a, and aac) files, and I've read blurbs online just a few years old stating some problem with the licensing of MP3 codecs costing more, and some browsers only supporting ACC? I don't think most home users keep up with browser updates. But I guess I could find an MP4 converter.
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...