Friday, February 18, 2011

New YouTube API for both flash and HTML5

             On January last week YouTube introduce a new API for embedding videos on web pages.  To embed a video, use an iframe with YouTube video URL as its src attribute.  The URL form is used in iframe to load the video.  For more details and examples refer 'A New Way To Embed YouTube Videos' posted on JULY 22, 2010.

             Now a YouTube video can be embed in web pages without the support of flash, since it uses the HTML5 also. Now most of the browsers support HTML5. This API is compactable for IOS devices (iPhone, iPad) also.  The YouTube uses HTML5 player rather than flash for mobile devices.

How to integrate the the new iframe API.

  1) Include the javascript library 'IFrame Player API code'.

      var tag = document.createElement('script');
      tag.src = "";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      This script will append the the API library to the head tag of the web page.

2) Add a container element to the web page, the iframe is is added to this      container element.

<div id=”myVideo” ></div>

3) Use the API script to create the iframe in the container element.

      var myPlayer;
      function onYouTubePlayerAPIReady() {
              myPlayer = new YT.Player('myVideo', {
                 height: '390',
                 width: '640',
                 videoId: 'u1zgFlCw8Aw',
                 playerVars: {
                     'start': 10
                events: {
                     'onReady': callBack1,
                     'onStateChange':  callBack2

The function onYouTubePlayerAPIReady is called by API while it is ready.  YT.Player  creates a javascript player object myPlayer and create an iframe inside the container element of specified id myVideo. It provides the options for customising iframe arrtibutes, player parameters   and  callback for Events from the player. we can use the js object myPlayer control the embed video throughout the js.
We can also embed video without a container element, include the iframe itself inthe webpage and use the API scrips. The element id is specified to the iframe.

<iframe id="myVideo" type="text/html" width="640" height="390"

                      Here we have to enable the API by specifing in the video url &

Here it solves the problems such as 'How to embed a YouTube video for iPad/iPhone ?' , 'YouTube API for iPad/iPhone', 'YouTube API for HTML5 player', YouTube API for mobile devices.  etc...



Note: This post is for my reference, donate me by clicking the adds.