Defining custom media types

You can allow custom media types by a simple modification to the site's web.config and MediaControl.ascx inline control. Any media types can be enabled to be recognized by the system in case that you have the right player for the file type.

 

The following example shows how to enable .flv videos in the system.

 

1. Add the following keys into the appSettings section of your web.config file. Add your custom extensions for the particular document types here for the files to be recognized as image/audio/video by the system, as we did with the highlighted flv extension.

 

<add key="CMSImageExtensions" value="bmp;gif;ico;png;wmf;jpg;jpeg;tiff;tif" />

<add key="CMSAudioExtensions" value="wav;wma;mp2;mp3;mid;midi;mpga" />

<add key="CMSVideoExtensions" value="avi;mp4;mpg;mpeg;wmv;qt;mov;rm;flv" />

 

2. Add the .flv extension to the enumeration in Site Manager -> Settings -> Media libraries -> Media file allowed extensions, which will allow upload of these files into media libraries.

 

You may also want to add the extension to the Site Manager -> Settings -> Media libraries -> Files upload extensions enumeration, which will allow upload of these files as CMS.File documents and document attachments.

 

3. Download a player for your media type. In this example, we will use the JW FLV Player available here: http://www.longtailvideo.com/players/jw-flv-player/.

 

4. Extract the swfobject.js and player.swf files and put them in a folder under your web site folder, e.g. ~/FlvPlayer. This path will be used in the CreateFlvObject() which we will create in step 5.

 

5. Open the ~/CMSInlineControls/MediaControl.ascx.cs inline control in Visual Studio and replace the Page_PreRender method with the following code. It is the original code of the method with the first condition added. The added code ensures that when the file extension is .flv, the CreateFlvObject() method is called.

 

protected void Page_PreRender(object sender, EventArgs e)

{

   if (this.Type.TrimStart('.').ToLower() == "flv")

   {

       CreateFlvObject();

   }

   else if (MediaHelper.IsFlash(this.Type))

   {

       CreateFlash();

   }

   else if (ImageHelper.IsImage(this.Type))

   {

       CreateImage();

   }

   else

   {

       CreateMedia();

   }

}

 

6. The last thing that we need to do is to add the CreateFlvObject() private method to the control, in this example, it looks as the code sample below.

 

The method creates a new DIV tag for the FLV player, with ID generated in stored the divID variable. It also handles the player URL in a special way - the URL must be absolute and must be ending with "flv". Support for the Autoplay and Loop properties of the player is also ensured.

 

private void CreateFlvObject()

{

   string divID = Guid.NewGuid().ToString();

   string flvUrl = UrlHelper.UpdateParameterInUrl(

               UrlHelper.GetAbsoluteUrl(this.Url), "ext", "flv");

   if (this.Loop)

   {

       flvUrl = UrlHelper.AddParameterToUrl(flvUrl, "repeat", "always");

   }

   if (this.AutoPlay)

   {

       flvUrl = UrlHelper.AddParameterToUrl(flvUrl, "autostart", "true");

   }

   string flvCode = "<script type='text/javascript' src='" +

               ResolveUrl("~/FLVPlayer/swfobject.js") + "'></script>";

   flvCode += "<div id='flv_" + divID + "'></div>";

   flvCode += "<script type='text/javascript'>";

   flvCode += "var s1 = new SWFObject('" +

               ResolveUrl("~/FLVPlayer/player.swf") + "','ply','" +

               this.Width + "','" + this.Height + "','9','#ffffff');";

   flvCode += "s1.addParam('allowfullscreen','true');";

   flvCode += "s1.addParam('allowscriptaccess','always');";

   flvCode += "s1.addParam('wmode','opaque');";

   flvCode += "s1.addParam('flashvars','file=" + flvUrl + "');";

   flvCode += "s1.write('flv_" + divID + "');";

   flvCode += "</script>";

   this.ltlMedia.Text = flvCode;

}

 

7. Now .flv files are recognized as videos by the system. The custom player is used for the files on the live site and also in the Insert image or media and Insert link dialogs, as you can see in the screenshot below.

 

clip0975