protected override void CreateChildControls()
{ try { base.CreateChildControls(); displayLabel = new Label();
if (Page.Request.QueryString["mediaurl"] == null ||
Page.Request.QueryString["mediaurl"].ToString().Equals(""))
{ displayLabel.Text = "set media url..."; }
else MediaURL = Page.Request.QueryString["mediaurl"].ToString(); Controls.Add(this.displayLabel); }
catch (Exception ex)
{ HandleException(ex);
}
}
protected override void Render(HtmlTextWriter writer)
{ this.EnsureChildControls(); try {if (!String.IsNullOrEmpty(MediaURL))
{String ext = MediaURL.Substring(MediaURL.LastIndexOf('.') + 1);
MediaPlayerFactory factory = MediaPlayerFactory.GetInstance();
MediaPlayer player = factory.GetMediaPlayer(ext);player.Height = PlayerHeight;
player.Width = PlayerWidth;
player.MediaURL = MediaURL;
player.ShowControls = ShowControls;
writer.Write(player.GetHTML());
this.displayLabel.RenderControl(writer); }else { //Render Error message in the labelHandleException(new Exception("set media url...."), writer);
}
}
catch (Exception e)
{ //handle Error eroperlyHandleException(e,writer);
}
}
Now, I'll explain the above code line by line.
String ext = MediaURL.Substring(MediaURL.LastIndexOf('.') + 1);
MediaPlayerFactory factory = MediaPlayerFactory.GetInstance();
MediaPlayer player = factory.GetMediaPlayer(ext); First, the file extension is extracted. then MediaPlayerFactory Singleton Object is instantiated. this factory will expose GetMediaPlayer method that returns a MediaPlayer object based on the file extension.
player.Height = PlayerHeight;
player.Width = PlayerWidth;
player.MediaURL = MediaURL;
player.ShowControls = ShowControls;
Player properties are set based on the web part properties.
writer.Write(player.GetHTML());
this.displayLabel.RenderControl(writer);Render the HTML in the Label.
Up to this point it is very straight forward and all related to web parts. All other areas of code is pure OOP. I hope you will understand the code.
Please note that I have tested the HTML <OBJECT> tag for Windows media player and not for the others. if you find any problems with other media players I am more than happy to assist.
Find the full code for this article at http://sites.google.com/site/bhakthil/Home/MediaWebPart.zip