Wednesday, December 21, 2011

Use the buit-in Silverlight media player inside a webPart - Sharepoint 2010

Adding a feature like playing a video when clicking over its name is something nice to add to your upcoming video webPart

All you have to do is:


  1. Add a container div with an ID (we need to use the ID later)
     <div id='videoContainer'></div> 

  2. Add a hyperlink inside the div which its 'href' links to the video you want to player on the silverlight player

  3. Add reference to the (mediaplayer.js) file, which can be found /_layout/mediaplayer.js
    e.g.:
     <script type="text/javascript" src="/_layouts/mediaplayer.js"></script>  

    Note: Make sure the script block placed after the div section

  4. Add the following script block
     <script type="text/javascript">  
          _spBodyOnLoadFunctionNames.push('mediaPlayer.createOverlayPlayer');  
          mediaplayer.attachToMediaLinks  
          (  
               (document.getElementById('videoContainer')),  
               ['wmv','avi','mp3']  
          );  
     </script>  

    This will be executed after the whole page finishes loading, plus it will replace each tag's onClick to trigger the silverlight player