Most Common Custom WebParts Part 3 – Windows OR YouTube Media Player WebPart

In my previous post you can view the most commonly used custom webparts, Tree View WebPart Shows Sites and Sub-Sites and Menu WebPart Shows Sites and Sub-Sites in Fly-Out Mode

Now I got one more chance to continue the series of most commonly used custom webparts, so once again I come up with a simple Windows Or Youtube Media Player webpart which plays video on Sharepoint sites, the webpart also supports to configure the properties as required.

In the webpart properties section please enter the Windows media or Youtube link, the webpart automatically detects the media type and plays accordingly

Download the solution file WindowsORYouTubeMediaWebPart.wsp

Windows Or YouTube Player WebPart (Playing Windows media Video)

Windows Or YouTube Player WebPart (Playing Windows media Video)

Windows Or YouTube Player WebPart (Playing Youtube Video)

Windows Or YouTube Player WebPart (Playing Youtube Video)


Windows Or YouTube Player WebPart Properties

Windows Or YouTube Player WebPart Properties

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace WindowsMediaWebPart.MediaWebPart
{
    [ToolboxItemAttribute(false)]
    public class WindowsMedia : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        //private const string _ascxPath = @"~/_CONTROLTEMPLATES/WindowsMediaWebPart/MediaWebPart/MediaWebPartUserControl.ascx";

        private string videoFilePath = string.Empty;
        private string videoWidth = "320";
        private string videoHeight = "240";
        private bool animationAtStart = true;
        private bool transparentAtStart = true;
        private bool autoStart = true;
        private bool showControls = true;
        private bool loopPlayBack = true;
        private string frameBorder = "0";

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Specifies the media file location"),
        Category("Media Properties"),
        WebDisplayName("Media File Location")]
        public string VideoFilePath
        {
            get { return videoFilePath; }
            set { videoFilePath = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Sets the width of the Media player"),
        Category("Media Properties"),
        WebDisplayName("Width")]
        public string VideoWidth
        {
            get { return videoWidth; }
            set { videoWidth = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Sets the height of the Media player"),
        Category("Media Properties"),
        WebDisplayName("Height")]
        public string VideoHeight
        {
            get { return videoHeight; }
            set { videoHeight = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Animation At Start: (True/False)"),
        Category("Media Properties"),
        WebDisplayName("Animation At Start")]
        public bool AnimationAtStart
        {
            get { return animationAtStart; }
            set { animationAtStart = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Transparent At Start: (True/False)"),
        Category("Media Properties"),
        WebDisplayName("Transparent At  Start")]
        public bool TransparentAtStart
        {
            get { return transparentAtStart; }
            set { transparentAtStart = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Auto Start: (True/False) Starts the video automatically"),
        Category("Media Properties"),
        WebDisplayName("Auto Start")]
        public bool AutoStart
        {
            get { return autoStart; }
            set { autoStart = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Show Controls: (True/False) Displays media player controls"),
        Category("Media Properties"),
        WebDisplayName("Show Controls")]
        public bool ShowControls
        {
            get { return showControls; }
            set { showControls = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Loop Play Back: (True/False) Repeats the video automatically"),
        Category("Media Properties"),
        WebDisplayName("Loop Play Back")]
        public bool LoopPlayBack
        {
            get { return loopPlayBack; }
            set { loopPlayBack = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Sets the border of the media (applicable for youtube videos)"),
        Category("Media Properties"),
        WebDisplayName("Allow Full Screen")]
        public string FrameBorder
        {
            get { return frameBorder; }
            set { frameBorder = value; }
        }

        protected override void CreateChildControls()
        {
            //Control control = Page.LoadControl(_ascxPath);
            //Controls.Add(control);
            this.TitleUrl = VideoFilePath;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SPSite site = null;
            SPWeb web = SPContext.Current.Web;
            if (!string.IsNullOrEmpty(VideoFilePath) && !string.IsNullOrEmpty(VideoWidth) && !string.IsNullOrEmpty(VideoHeight))
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        site = new SPSite(web.Site.ID);

                        using (site)
                        {
                            if ((videoFilePath.ToLower()).Contains("youtube.com/"))
                            {
                                Uri youtubeUri = new Uri(videoFilePath);
                                string query = youtubeUri.Query;
                                string mediaID = HttpUtility.ParseQueryString(query).Get("v");
                                string source = "http://www.youtube.com/embed/" + mediaID;
                                writer.Write("<iframe width='" + VideoWidth + "' height='" + VideoHeight + "' src='" + source + "' frameborder='" + FrameBorder + "' allowfullscreen></iframe>");
                            }
                            else
                            {

                                writer.Write("<OBJECT id='mediaPlayer' width='" + VideoWidth + "' height='" + VideoHeight + "' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>");
                                writer.Write("<param name='fileName' value='" + VideoFilePath + "'>");
                                writer.Write("<param name='animationatStart' value='" + AnimationAtStart + "'>");
                                writer.Write("<param name='transparentatStart' value='" + TransparentAtStart + "'>");
                                writer.Write("<param name='autoStart' value='" + AutoStart + "'>");
                                writer.Write("<param name='showControls' value='" + ShowControls + "'>");
                                writer.Write("<param name='loop' value='" + LoopPlayBack + "'>");
                                writer.Write("<EMBED type='application/x-mplayer2'pluginspage='http://microsoft.com/windows/mediaplayer/en/download/'id='windowsmediaPlayer' name='windowsmediaPlayer' displaysize='4' autosize='-1' bgcolor='darkblue' showcontrols='" + ShowControls + "' showtracker='-1' showdisplay='0' showstatusbar='-1' videoborder3d='-1' width='" + VideoWidth + "' height='" + VideoHeight + "' src='" + VideoFilePath + "' autostart='" + AutoStart + "' designtimesp='5311' loop='" + LoopPlayBack + "'></EMBED>");
                                writer.Write("</OBJECT>");

                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    writer.Write(ex.Message);
                }
            }
            else
            {
                writer.Write("<span class='ms-formvalidation'>");
                writer.Write("Please configure the <B>Media WebPart</B> properties in webpart properties section");
                writer.Write("</span>");
            }
        }
    }
}