Larry Bouthillier's Streaming Media Player Detection Tutorial - server-side JSP code
Listing One - playVideo.jsp (scroll down for Listing Two)
001 <%@ page import="PlayerDataObject"%><%@ page contentType=""%><%
002
003 // check for List of Available Players cookie
004 String theCookieVal=null;
005 Cookie[] cookies = request.getCookies();
006 for(int i=0;i<%
014 }
015 // if we've got the MediaPlayers cookie, then we must have the MediaThroughput
016 // cookie as well - both are set in detectPlugin.jsp
017 theCookieVal=null;
018 int mediaThroughput=0;
019 for(int i=0;i 1000 ) { bandwidth = "BROADBAND"; }
033 else if ( mediaThroughput > 200 ) { bandwidth = "CABLE-DSL"; }
034 else if ( mediaThroughput > 35 ) { bandwidth = "56k"; }
035 else if ( mediaThroughput > 18 ) { bandwidth = "28k"; }
036 else if ( mediaThroughput > 10 ) { bandwidth = "14.4"; }
037 else bandwidth = "forget it";
038 // set this in a session variable so that it's available to
039 // other pages and jsp's in our site
040 session.putValue("ConnectionType",bandwidth);
041
042
043
044 // check for selected player cookie
045 theCookieVal=null;
046 for(int i=0;i<%
054 }
055
056 // now let's get the clip parameter and pass it to a playerMetadata object
057 String clip = request.getParameter("clip");
058 if ( clip==null ) {
059 %><%
060 }
061
062 PlayerDataObject playerData = PlayerDataObject.getPlayerDataObject(theCookieVal);
063 if ( playerData==null ) {
064 %><%
065 }
066
067 // now we have a playerData object - one that knows the mimetype and
068 // how to create the metafile for that player type.
069 System.out.println("mimetype is" +playerData.getMimetype());
070 // first set the mimetype for the player we're using
071 response.setContentType(playerData.getMimetype());
072 // then tell the playerData object which clip we want
073 playerData.setClipname(clip);
074 // finally, get the metafile and print it to the output stream
075 // to the client's browser
076 out.println(playerData.play());
077 %>
Listing Two - detectPlugin.jsp
001 <%@ page import="PlayerDataObject"%>
002 <%
003 // detectPlugin.jsp
004
005 // set this page to not be cached by the browser
006 response.setHeader("Pragma", "No-cache");
007 response.setDateHeader("Expires", -1);
008 response.setHeader("Cache-Control", "no-cache");
009
010 /* --------------------------------------------------
011 * Code between the <% and %> is Java that's executed on the server.
012 * All other code is treated like any other HTML page and
013 * is sent to to the browser.
014 * --------------------------------------------------*/
015
016 boolean isActiveX=false;
017 boolean isPlugin=false;
018 String mUserAgent = request.getHeader("User-Agent");
019
020 // see if this is MSIE 4.+ on Windows - this is primitive!
021 if( mUserAgent.indexOf("MSIE") != -1 && mUserAgent.indexOf("Win") != -1 ){
022 isActiveX=true;
023 } else { // else it's plugin architecture
024 isPlugin=true;
025 }
026
027 %>
028
029
030
031
032 Browser Preference Test
033
034 <%
035 // Now send the client-side script appropriate for the typeof browser/platform wehave
036 if(isPlugin){
037 //here's the plugin code
038 %>
039
091
095
103
110 <%
111 }
112 if(isActiveX){
113 //here's the activeX code
114 %>
115
128
149
166
170
178
185 <%
186 }
187 %>
188
Listing Three - selectPlayer.jsp
001 <%@ page import="PlayerDataObject"%>
002 <%
003 // selectPlayer.jsp
004 // Displays an HTML form that lets users pick their favorite media player
005
006 boolean hasReal=false, hasWMP=false, hasQT=false;
007
008 // check for cookie
009 String theCookieVal=null;
010 Cookie[] cookies = request.getCookies();
011 for(int i=0;i
020
021 <%
022 }
023
024 if ( theCookieVal.indexOf(PlayerDataObject.REAL_TYPE) != -1 ) {
025 hasReal=true;
026 }
027 if ( theCookieVal.indexOf(PlayerDataObject.WMP_TYPE) != -1 ) {
028 hasWMP=true;
029 }
030 if ( theCookieVal.indexOf(PlayerDataObject.QT_TYPE) != -1 ) {
031 hasQT=true;
032 }
033 %>
034
035 Select your preferred media player
036
037
052
053
054
055 Choose your preferred media player
056
070
Your connection type appears to be <%= session.getValue("ConnectionType") %>
071