Capt. Horatio T.P. Webb
 
Web Design with the Youtube Player -- Spring 2016

Version 1 -- 3/20/2016

www.youtube.com contains the great collection of video on the internet. It usage rates trounce any other video source site. It is common to the entire world of Internet users and as much a part the typical users Google experience as their search engine.

The central technology of the site is the flash player which is invoked by the user when a video is requested from youtube.com. This Adobe product had been around for years and is a standard for video display. Conversion from various media to the flash file format (called *.swf files) requires the use of various Adobe products to get the media into the correct video format for the flash player. youtube.com has done this task the user and the only thing to user need to do is invoke that flash player and download the *.swf file from youtube.com.

It has been a standard policy at youtube.com to provide for each video in their inventory a small snippet of code that allows anyone to "embed" a youtube video on to any web page. To find this code snippet. Load any youtube video into your browser and below the video you will see an arrow followed by the word "SHARE". If you click the "SHARE" another menu appears with a second link that says "EMBED". Click this link and in a textbox the one line the HTML code appears -- such as this:

CAUTION:

This demonstration will NOT work in a mobile environment! On both IPhone and Android

It is will only work from a desktop

This webpage is designed to allow a user to choose from a music list and then start playing the chosen musical sellection. However, this requires the code to utilize and "autostart" which has been disabled by the mobile device manufacturers. Their rational is:

Autoplay: Not supported on iPhone or Android; supported on all desktop browsers, iPad, Firefox and Opera for Android.

or

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it.

or

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.

The HTML says to place an iframe (a container with some HTML inside it which can appear anywhere on a webpage). This iframe:

  1. loads from www.youtube.com an "embedded" flash player
    this player comes with video and audio controls at the bottom of the video where you can pause, play, change the location by dragging in the progress bar, a full screen button and a ton of advertising.
  2. the size of the flash player size is set to: 420 by 315 pixels
  3. then it loads and plays the video specified with the unique 11 byte identifier -- in this case: ZX0CfFdk-jw

If you know the video's identifier, you can do more things than take it as youtube.com gave it to you.

What follows is a general discussion of how to customize a flash player.

The "swfobject" (the player for the *.swf files) is created by:

swfobject.embedSWF(swftoplay, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes)

Of the following attributes, the first 5 are ALL REQUIRED:

  1. swfUrl specifies the URL and filename of your video
  2. id specifies the id of the HTML element to contain the video (see the next step)
  3. width specifies the width of your video
  4. height specifies the height of your video
  5. version specifies the Flash player version your video (format is: "major.minor.release")
  6. expressInstallSwfurl specifies the URL of your express install SWF and activates Adobe express install
    [see http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b75].
    The express install will only fired once (the first time that it is invoked),
    that it is only supported by Flash Player 6.0.65 or higher on Win or Mac platforms,
    and that it requires a minimal SWF size of 310x137px.
  7. flashvars specifies your flashvars with name:value pairs

    we will not use flashvars in this example but we will add this empty definition before the swfobject.embedSWF as we have to pass something as the values of the flashvars (in this case nothing)

    var flashvars = {};

  8. params specifies your nested object element params with name:value pairs

    The params list of options is:

    As the second optional part of the swfobject.embedSWF we can specify the params like this:

    var params = { parameter_name_1 : "value" , parameter_name_2 : "value" , parameter_name_3 : "value" , .
    .
    .
    };

    For example:

    var params = {
    autostart: "true" ,
    menu: "false" ,
    loop: "true" ,
    allowscriptaccess: "always",
    };

  9. attributes specifies your object's attributes with name:value pairs. As the third part of the swfobject.embedSWF we can specify the attributes like this:
    1. id user defined id for the SWF player
    2. name user defined name for the palyer
    3. styleclass CSS class name for the player

    We seldom use the attribute and rely on the params for most customization

Here is a complete javascript example:

var flashvars = {};
var params = {
          autostart: "true" ,
          menu: "false" ,
          loop: "true" ,
          allowscriptaccess: "always" ,
          };
var attributes = {};
swfobject.embedSWF(

          "http://URL/path/swf filename.swf",
          "fout",
          "360", "270",
          "9.0.0","expressInstall.swf", flashvars, params, attributes);

Then on the HTML page would be the DIV with the id (the second parameter of the swfobject.embedSWF -- in this case the name coden was "fout") like this:

The purpose here is to demonstrate the degree of control you have over the youtube player in a standalone environment you create yourself.

But with youtube, Google recommends (insists) you use their player definition that comes with the "embed" tag demonstrated above.

See what Google says here about the way it load the HTML iframe from the recommended "embed" that comes with each youtube video:

https://developers.google.com/youtube/iframe_api_reference

Google provides a script to use in your HTML pages. It runs in-line (i.e., as the page is loaded and parsed the code is executed as encountered). Here is their recommended use of the embeded youtube video:

The code/HTML above:

  1. creates a empty DIV block (in this case with id="player")
  2. creates a script tag with id="script"
  3. loads their javascript into the script and executes it
  4. it first finds the parent of the script tag and inserts a new tag to contain the player that replaces the parent
  5. once all this has been done (i.e, the player is created,
  6. and the video downloaded started,
  7. The code waits until the video is ready to play and then
  8. fires the onPlayerReady event. This function:
    1. starts the video (event.target.playVideo();)
    2. and then sets a switch called done to true when it is finished

This is OK for a single embed for a single youtube video. But we have grander aspirations. What we will do is make a playlist (i.e., a lot of videos that our user can pick from) and annotate each video (i.e, provide some additional text to show along side the video). We will you my top 30 Bob Dylan songs as our video playlist and show the lyrics as the songs play (a necessary aid as the lyrics are complicated and are not sometimes easy to pick out over the music and Dylan's often emotional verbal expression)

The live version of the page is here:

http://www.bauer.uh.edu/parks/utest2.htm

Go to the page and watch and listen. Click on any of the 30 songs in the blue list below the player. There are also a few straight .mp3 audio files that did not come from youtube. We will deal with these audios differently.

Here is complete annotated code (shortened to make it easier to understand)