Free Silverlight 1.0 Media Player

April 24, 2008 11:23 by John M

Hey everyone we decided to open our xLite Player and offer it for free!  You get all of the source code that it takes to make the xLite Player work. 

We have combined the files that are used with Expression Encoder and also our own .aspx pages/xaml/js files to create a lite media player that can easily be integrated into websites. 

Also it comes with a management page so a non-IT person can manage and upload the videos!

UPDATED ON 04-29-08: If you do use this on your website give me the link so I can post it here that way others can see live examples of this as well.  Also, I added this project to CodePlex.  You can view the project at http://www.codeplex.com/xLitePlayer.  If you would like to be a contributor please email me and we can go from there.

Check out the product page to download it now!

UPDATE ON 05-23-08: Hey everyone we have had excellent download traffic for this player, but litte feedback on it.  Feel free to provide feedback as we want it so we can make this better for people.  Again if you are using it on a website that is public please let us know so we can post it here or on codeplex so people can see usage of it.  Thanks!


Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Dynamically Loading Images in SilverLight 1.0 using C#

April 18, 2008 09:40 by Onslaught

Here is an easy way to dynamically load images from a directory into your SilverLight 1.0 application using C#. I'm going to assume you already know how to set up your application to use an aspx (with codebehind) page instead of a static HTML page. If not, this article may be useful.

The aspx page (I'll call it sampleXAML.aspx) contains your XAML declaration for the SilverLight 1.0 application. For this elucidation, we will only have one main canvas. In it, we will call your loadImages() method from the codebehind.

<Canvas>
  <%= loadImages() %>
</Canvas>

In your codebehind (sampleXAML.aspx.cs), we dynamically load images that will be displayed in the SilverLight app. First will create a string variable that will house our XAML declaration for an image.

string strImages = @"<Image x:Name=""image-{0}"" Source=""{1}"" Canvas.Left=""{2}"" Canvas.Right=""{3}"" />;

I've included Canvas.Left and Canvas.Right so images will be placed in a cascading fashion to ensure they were all loaded.

Keep in mind you will need the following namespaces:

System.IO; 
System.Text; 


Next step, read in your files from a directory using the loadImages() method...

protected string loadImages()
{
  StringBuilder strXML = new StringBuilder();

  int intCounter = 0;

  DirectoryInfo dirs = new DirectoryInfo(MapPath("~/images")); //Change path here for a specific folder you want to read in from

  foreach (FileInfo fi in dirs.GetFiles("*.jpg"))
  {
    strXml.AppendFormat(strImages, intCounter, "images/" + fi.Name, intCounter*100, intCounter*100);
    intCounter++;
  }
 
  return strXML.ToString();
}

The strXML is used to build your <Image> XAML tag and is populated in the foreach loop by four parameters. You can add or remove parameters as needed, but at mininum you'll need the Source.

Note that I've filtered for JPGs. First, SilverLight 1.0 can only read JPGs and PNGs. Secondly, Windows loves to create the thumbs.db to display the cool little folder with tiny images of the pictures. While you may not get an error, it will attempt to create another <Image> XAML tag that is empty for that thumbs.db file.

When you run your application, the XAML generated should read to something similar:

<Image x:Name="image-0" Source="images/img01.jpg" Canvas.Left="0" Canvas.Right="0" />
<Image x:Name="image-1" Source="images/img02.jpg" Canvas.Left="100" Canvas.Right="100" />
<Image x:Name="image-2" Source="images/img03.jpg" Canvas.Left="200" Canvas.Right="200" />
<Image x:Name="image-3" Source="images/img04.jpg" Canvas.Left="300" Canvas.Right="300" />

The end. Post any questions you may have.


Currently rated 4.3 by 4 people

  • Currently 4.25/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Run Server Side Code from Silverlight objects

February 13, 2008 19:37 by John M

This article will describe how you can make AJAX callbacks when using Silverlight.  Pretty much what this is, is a concept on how when you handle and event in your XAML it will run some server side code.  This example will provide steps you need to accomplish this and assume you are running ASP.NET 2.0:

Step 1) Have your object in your xaml that has and event tied to it.  For example in your XAML you may have something like this:

<Image Source="/images/pic1.jpg"
       Tag="myPic"
       x:Name="myPicXName"
       Width="100"
       Height="100"
       Canvas.Top="20"
       Canvas.Left="8"
       Opacity=".5"
       MouseLeftButtonDown="leftDownClick" />

Step 2) Have a function in one of your javascript files named what we named it in Step 1 for the MouseLeftButtonDown property.  In addition, have the function you want to run when the call back is done.  For example:

function leftDownClick(sender, args)
{
 
   var objSender = sender;
   var objTag = sender["Tag"];  
   var objName = sender["Name"];
   
   //this function is located on default.aspx
   doCallBack(objName);
 
}

//this is called from default.aspx.cs
function runClientCallBack(message, context)
{
    //message is the returnValue variable in default.aspx.cs
    //do whatever you want
}

Step 3) Have javascript functions your main ASPX page. For the purpose of this article will say on your default.aspx page. So for example your page may look something like this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script type='text/javascript' src="js/Silverlight.js"></script>
    <script type='text/javascript' src="js/createSilverlightMain.js"></script> 

    <script type="text/javascript">
       
        //this gets called from createSilverlightMain.js
        // in leftDownClick
        function doCallBack(name)
        {
           var message = name;
           var context = '';
          
           //this is located in default.aspx.cs              
           <%=sCallBackFunctionInvocation%>
        }
       
        //this is used for ICallbackEventHandler
        //in the Page_Load
        function OnError(message, context)
        {
           alert('An unhandled exception has occurred:\n' + message);
        }
       
    </script>

</head>
<body>
    <form id="form1" runat="server">
       <div id="SilverlightHost" style="width:650px; height:425px;">
           <script  type='text/javascript'>
     createSilverlight();       
    </script>
       </div>

    </form>
</body>
</html>

Step 4) Handle the call back in your default.aspx.cs file:

1 - you need to have default.aspx.cs inherit the following:

System.Web.UI.ICallbackEventHandler

You can do this in the top where it looks something like this:

public partial class _default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler


2 - Use the code sample below in your default.aspx.cs file:

        protected string returnValue = "default";
        public string sCallBackFunctionInvocation;

        protected void Page_Load(object sender, EventArgs e)
        {
            sCallBackFunctionInvocation = this.Page.ClientScript.GetCallbackEventReference(this, "message", "runClientCallBack", "context", "OnError", false);
        }

        public void RaiseCallbackEvent(String eventArgument)
        {

           //whatever code you want to run

           returnValue = //whatever string value you want

           //you could even write out another xaml object here in a string
        }

        public string GetCallbackResult()
        {
            return returnValue;
        }

 

This concept should get you started.  This still doesn't let you access silverlight objects directly in code behind but does give you the ability to run server side code from a silverlight object. you'll notice on the initial leftDownClick button I pass the name property from the silverlight object.  This lets me keep track of what object is making the callback.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Silverlight Media Player

February 9, 2008 12:54 by John M

 

04-23-2008:  This post has been updated to inform our readers to read this post below:

http://www.xdevsoftware.com/blog/post/Free-Silverlight-10-Media-Player.aspx

Thanks everyone,

John


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Setup Silverlight Streaming Service with StartPlayer.js

February 8, 2008 16:30 by John M

XDev Software has adopted using the free service provided by Microsoft called Microsoft Silverlight Streaming.  I wanted to provided a quick overview on how one can setup their javascript file to utilize this service once one has signed up for it.  For this example I have used the StartPlayer.js file that comes with Microsoft’s new software called Microsoft Expression Encoder.  I read an article from here

This article explains how Expression Encoder comes with base javascript files that handle all of the base functionality of a media player for Silverlight.  There are two core files that come with Expression Encoder templates.  They are BasePlayer.js and StartPlayer.js.  There are 3 more files that come with it, one called MicrosoftAjax.js, player.js and PlayerString.js.  However, you actually only need to deal with StartPlayer.js because in the end you just need to know the functions to call from BasePlayer.js and so forth.  I strongly recommend you read the article in the link above to get a better understanding.

Anyway, off to the point of the article.  Ok once you have signed up for the Silverlight Streaming Service you will need to modify the StartPlayer.js file and reference a new javascript file instead of a local Silverlight.js.  The silverlight.js file at the time of this article that you need to reference is the following:

http://agappdom.net/h/silverlight.js

I have copied my code below so you can see how to modify the StartPlayer.js file.  Please note I have made additional modifications besides just getting it to work with the Silverlight Streaming Service.  I further modified it to allow myself to pass a media path as a variable.  I may write another blog on that at another time.  However, for right now I have sections dedicated to local media viewing and media viewing using Silverlight Streaming Service for local developing purposes.  Below is my full StartPlayer.js file

------------------------------------------------------------------------------------

var strMovie = ""; //user added variable

//we added a parameter to StartPlayer_0, get_mediainfo, _playNextVideo
 
function get_mediainfo(mediainfoIndex, strMovie) {
    switch (mediainfoIndex) {       

        case 0:
            return  { "mediaUrl": strMovie,
                      "placeholderImage": "",
                      "chapters": [              
                                  ]
                    };                                                               
                         
        default:
             throw Error.invalidOperation("No such mediainfo");
     }
}

function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}

function StartPlayer_0(parentId, movie) {
    strMovie = movie;
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
   
    //STREAMING SILVERLIGHT OBJECT  
    Silverlight.createHostedObjectEx(
 {   source: 'player.xaml',
          parentElement: $get(parentId ||"divPlayer_0"),
          id:this._hostname,
          properties:
          { width:'100%'
           , height:'100%'
           , version:'1.0'
           , isWindowless:'false;'
           , background:'#383838'
          },
          events:
          {
            onLoad:Function.createDelegate(this, this._handleLoad)
          },
          initParams:[strMovie]
        }
   );
    //NON STREAMING SILVERLIGHT
//    Silverlight.createObjectEx(
// {   source: 'player.xaml',
//          parentElement: $get(parentId ||"divPlayer_0"),
//          id:this._hostname,
//          properties:
//          { width:'100%'
//            , height:'100%'
//            , version:'1.0'
//            , isWindowless:'false;'
//            , background:'#383838'
//          },
//          events:
//          {
//             onLoad:Function.createDelegate(this, this._handleLoad)
//          }
//      }
//    );
    this._currentMediainfo = 0;
}
StartPlayer_0.prototype= {
    _handleLoad: function() {
        this._player = $create(   ExtendedPlayer.Player,
                                  { // properties
                                    autoPlay    : true,
                                    volume      : 1.0,
                                    muted       : false
                                  },
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  );
                                 
        this._playNextVideo(strMovie);    
    },   
    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout( Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Ee.UI.Xaml.Media.Res.mediaFailed, this._player.get_mediaUrl() ) );
    },
    _playNextVideo: function(movie) {
   //used for streaming
        var params = $get(this._hostname).InitParams;
        var cVideos = 1;
        if (this._currentMediainfo<cVideos)
        {
            //used for local
           // this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++, movie ) );
            //used for streaming
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++, params ) );
        }      
    }       
}


NOTE: the media path that gets assigned to strMovie may look something like this:

streaming:/yourSilverlightStreamingID/yourSilverlightAppName/yourVideo.wmv

I know in the article noted above it mentions something on how Microsoft is hoping that people adopt the javascript classes/files they have made so people can create their own media players and media player skins so we have done just that.

Again, this is only one way to setup Silverlight Streaming service using the StartPlayer.js file.  However, if you don’t want to use the variable strMovie you can replace it with the static media path similar to the one mentioned above.

I know this may be a lot to take in at once, so please, any questions let us know.

 

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5