Monday, February 27, 2012

SharePoint 2010 VIDEO 
[ How to play it without using any third party tool ]

In my first SharePoint project, there was a requirement to play a video which has been added into the assets library (basically you can upload videos into this [will talk more about this type later] ) with in a custom web part (visual web part). playing video through the web is not a big deal, but in here requirement was, you cannot use any third party tool (like Flash player) on this.

If you are work in SharePoint 2010, you can use mediaplayer JavaScript to do that (I'm not sure that same JS is exist in SharePoint 2007 or 2003 ).

How to DO that ??


First you need to add hyper link to bind the video link (which taken from assets library). then put it in side a div tag, which ID should be "mediaplayer"



<div id="mediaplayer">

   <asp:HyperLink ID="HyperLinkVideo" runat="server">
                    <asp:Label ID="labelNewsVideo" runat="server"></asp:Label>
   </asp:HyperLink>

</div>


In here you can see that I have put my hyperlink in side a div tag which ID is mediaplayer (this is mandatory because, in the mediaplayer.js, identify the video link through this). and label (asp:Label) is use to show the video title or name.
then first of all you need to bind the video link and the name in to the required fields.


                try
                {
                    SPWeb rootWeb = SPContext.Current.Web;
                    // create sharepoint list object to get the newsletter data
                    SPList vidlist = rootWeb.Lists[VID_LIB_NAME];

                    // configure the query with the List item collection
                    SPListItemCollection vidItems = vidlist.GetItems(query);
                    // check whether the first item is null
                    mediaplayer.Visible = false;
                    ErrorMsg.Visible = false;
                    if (vidItems.Count != 0)
                    {
                        mediaplayer.Visible = true;
                        // display the required detail.
                        labelNewsVideo.Text = (vidItems[0]["Title"]==null) ? vidItems[0].Name : vidItems[0]["Title"].ToString();
                        // set the Document URL in to the link
                        HyperLinkVideo.NavigateUrl = vidItems[0][SPBuiltInFieldId.EncodedAbsUrl].ToString();                      
                      
                    }
                    else
                    {
                        ErrorMsg.Visible = true;
                    }
                }
                catch (Exception ex)
                {

                    ErrorMsg.Visible = true;
                }


you can see how to get the video link and assign it into the hype link through the hi-lighted area.

Then you need to implement JavaScript part in to the code. please refer the following code block.


<script type="text/javascript" src="~sitecollection/_layouts/mediaplayer.js"></script>
    <script type="text/javascript">
        _spBodyOnLoadFunctionNames.push('mediaPlayer.createOverlayPlayer');
        mediaPlayer.attachToMediaLinks(document.getElementById('mediaplayer'), ['wmv', 'avi', 'mp3']); 
    </script>


First You need to bind the mediapayer.js file with the web part, then use following code to push the required video detail in to the JS function (refer the hi-lighted are ). also you can define which type of videos that you need to play (in here i have assign wmv,avi and mp3)so all-together code block is like this.


<td class="WB_FormatHeading">
            <div id="mediaplayer">
                <asp:HyperLink ID="HyperLinkVideo" runat="server">
                    <asp:Label ID="labelNewsVideo" runat="server"></asp:Label></asp:HyperLink></div>
        </td>
    </tr>
    <script type="text/javascript" src="~sitecollection/_layouts/mediaplayer.js"></script>
    <script type="text/javascript">
        _spBodyOnLoadFunctionNames.push('mediaPlayer.createOverlayPlayer');
        mediaPlayer.attachToMediaLinks(document.getElementById('mediaplayer'), ['wmv', 'avi', 'mp3']); 
    </script>


Now you ready to go. after to deploy this.


when you click the video link it'll play with nice popup (good part is you don't need to install any third part tool like flash player).

ok That's It ... 
HAPPY CODING


1 comment: