Home | Pauline's Pages | Howto Articles | Uniquely NZ | Small Firms | Search |
Embedding Video in Web pages with useful JavaScript snippets |
Video is arguably one of the most difficult things to include on a web page for several reasons, firstly a lack of common standards, secondly the huge data rate requirement and thirdly the processing power. So why bother? The rationale was that a picture is worth a thousand words and in some cases, like flame pictures, a video is worth a score of pictures. Unfortunately the data required is in the same ratio. A picture will easily take 8 Kbytes, the same as a thousand words of text and a 320x240 video clip of even ten seconds can take 200 Kbytes, roughly a 20 fold increase over a similar size static graphic.
Statistics useful to make compromises: The statistics for the Theale Fireplaces web site have been gathered over a period of 18 months using an "Intelligent Counter" from TheCounter.Com and show 83% using Internet Explorer 5 or higher with another 6% using IE4 and 5% using Netscape 4 or higher. Operating systems show Windows 98 66% followed by Windows 95 15% and NT/2000 12%. These figures are an average over the life of the site and almost certainly the current figures will be more biased towards IE 5/6 and Windows 98/2000. The conclusion is therefore to offer fully integrated video support for Internet Explorer 5+ and a way for those with IE4 and Netscape 4.7x to see the videos, even if not fully integrated. The expectation is that this approach will mean that initially 85% of visitors will be able to see fully integrated video and 95% should be able to see video in some form. This, of course, assumes that JavaScript is enabled - there is a trend towards disabling JavaScript to prevent popup adverts and users may need to be alerted to the need to re-enable JavaScript to see the Videos. Currently only 1% of Theale Fireplaces visitors have disabled JavaScript.
The choice is difficult as one does not want to provide even more support to the Microsoft monopoly and the last versions (7.x) of Media Player are gross in their stand alone versions. RealPlayer is little better and is intrusive in the way it changes attributions and has a potential "call home" feature revealing ones browsing etc habits when it is online. Apple Quicktime seems to just do the job but is less supported. My video package can output to a number of Media Player supported formats including .avi and .wmv and to RealPlayer .rm but does not have a direct output in Quicktime format. Media Player is, not surprisingly, the easiest to embed into pages for Internet Explorer as an ActiveX Object and Microsift claim it should be equally well supported as a plugin they supply for Netscape Navigator. The decision had to be for primary support to be for Media Player keeping options open to support RealPlayer formats at a latter stage. Whilst streaming video seems atractive there are advantages in initially working with ordinary video which can be held in cache to be replayed or repeated. Streaming video also requires additional software on the server which is not supported free of charge by many ISPs.
The more professional approach is to embed the player which keeps some control over the display. I have tried both embeding into a normal page and into a popup window. The latter has the advantage that it can be left to download the video and then returned to when completed. To cut a long story short it is possible to leave the Popup Window minimised or behind other windows (blured in the jargon) and then bring it to the foreground (into focus) when the download completes using some clever JavaScript.
<OBJECT ID="MediaPlayer" width=320 height=310
classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
codebase="http://activex.microsoft.com/activex/controls/
mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby=
"Loading Media Player"
type="application/x-oleobject">
<PARAM NAME="FileName" VALUE="video/ceramica.avi">
<PARAM NAME="AutoStart" Value="True">
<PARAM NAME="ShowControls" VALUE="True">
<PARAM NAME="ShowStatusBar" VALUE="True">
<EMBED type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" SRC="video/ceramica.avi" width=320 height=310 name="MediaPlayer" autostart="True" ShowStatusBar=1 ShowControls=1 >
</EMBED>
</OBJECT>
And the result is:
<FORM name="MediaForm">
<SELECT NAME="VidChange" onChange="{document.MediaPlayer.FileName = document.MediaForm.VidChange.options[document.MediaForm.VidChange.selectedIndex].value}">
<OPTION VALUE="video/ceramica.avi">Select a New Flame Picture
<OPTION VALUE="video/pebble_and_driftwood.avi">Pebbles and Driftwood
</SELECT>
Loop:
<INPUT TYPE="checkbox" NAME="VidRepeat" onClick="{if ( document.forms.MediaForm.VidRepeat.checked == true){document.MediaPlayer.PlayCount = 999}else {document.MediaPlayer.PlayCount = 1 }}">
</FORM>
This way of setting parameters will fail or give a JavaScript error in Netscape which uses a different way of changing the parameters using get and set methods. The following examples are from the MSDN library show how to retrieve and set the FileName property from Netscape Navigator:
MediaPlayer.SetFileName("demo.avi");
var myTitle = MediaPlayer.GetFileName();
instead of the corresponding statements in Internet Explorer which are:
MediaPlayer.FileName = "demo.avi"
var myTitle = MediaPlayer.FileName
To write a script that works under both browsers one must check the browser type before accessing the properties. This page actually does it properly by calling a function when the onChange or onClick event happens which contains code to check if the Browser is Netscape then either change the value or call a SetXxxx() function. Rather than have too much confusing code on the page it is best to view the source to see what I have done.
More Media Player Documentation: There is a wealth of information on how to control the Microsoft Media Player on The Microsoft MSDN library site at http://msdn.microsoft.com/library/. The first caution is that it mostly refers to Version 7.1 whilst it is safer to code for Version 6.4. Version 7.1 uses a new and rather different Document Object Model via a different ClassID (CLSID) which is not compatible with 6.4 whilst it is safe the other way round if you use the older 6.4 CLSID which is given above which forces compatibility if 7.1 is installed. It is not possible to give a simple URL on MSDN - you currently need to navigate via menus to the MSDN Library -> Graphics and MultiMedia -> Windows Media Technology -> Windows Media Player 7.1 then back to 6.4.
Properties of the navigator object: The only property which is present in all browsers and which provides sufficient information to extract a version number is the appVersion property. For Internet Explorer one needs to extract the version from the characters following MSIE. The table of properties above is generated using a JavaScript for(var Property_Variable in object) {statements} loop for the navigator object - view the Source Code of this page to find out how to use this very useful technique for debugging or just listing the available properties of an object. I use the following JavaScript to pull out the full version of MSIE in use.
<script language="JavaScript" type="text/javascript">
var full_version = 0
var ie_start = navigator.appVersion.indexOf("MSIE")
if (ie_start != -1) {
var version_string = navigator.appVersion.substring(ie_start + 5)
full_version = parseFloat(version_string)
}
</script>
This delivers full_version as
for your browser which allows us to select only Internet Explorer Browsers which are greater than say 5.0.
We are now getting close to being able to deliver what is required. The secret is to use the JavaScript to identify the Browser and then selectively write the HTML code for the page using a series of document.writeln("HTML string") statements - remember to put a \ escape character in front of any " quotes to avoid some very strange problems!
var full_file_name = ""
var file_type = ""
var file_body = ""
if (location.search) {
full_file_name = location.search.substring(1)
var ext_start = full_file_name.indexOf(".")
if (ext_start != -1) {
file_type = location.search.substring(ext_start + 1)
file_body = location.search.substring(1 , ext_start + 1)
}
}
The following table shows the properties of the location object:
Copyright © Peter Curtis Content revised: 4th December 2001 |