Advertisement

Using Silverlight Media Framework for simple playback

If you aren’t aware of the Silverlight Media Framework, you should take a look.  This is a media playback framework for Silverlight that is based off of a lot of best practices from such implementations as the NBC Olympics, Sunday Night Football and others. 

Silverlight Media Framework screenshot

It has a lot of features built-in to the framework such as:

  • Logging
  • DVR-style features
  • Fast forward
  • Slow motion
  • Media Markers
  • etc

Basic stuff plus some great included features and extensibility points.

Missing Features – Part 1

What I didn’t like in v1 was two things: it was only for Smooth Streaming and it was a framework versus just a XAP I could use in a web application.  After some successful complaining :-) and an opportunity to get into a milestone build, the progressive download feature was added which enabled non-Smooth Streaming people to use it.

I’m wanting to standardize on what our teams are providing for best practices, so I’ve started using this player. 

NOTE: Does SL Video Player still live?  Yes, and it has VERY basic features.  It is super small and simple, but may not be for everyone’s liking.

So I started to solve the other problem, primarily for my use, of having essentially a stand-alone player using this framework. 

Extending the Silverlight Media Framework

You see, the SMF itself is essentially a set of controls…but not an ‘app’ itself that you can just consume the binary.  What I did was basically create a new Silverlight application myself with one simple element: Player.  This way I could implement what I needed for my use.  The first thing I wanted was to have a simple XAP that I’d be able to load parameters in…very much like we did for SL Video Player on codeplex.  To make essentially the player have a flexible use model.  I could host the player anywhere and just feed it media to play.

I used the InitParams feature of the Silverlight plugin model to enable me to pass in parameters to the application.  I wanted a simple parameter ‘media’ that basically was a URI to my media.  For most of my needs this would be a progressive download situation.  I added the simple feature using InitParams, and passed that URI to the MediaElement of the player framework.  All was well.

Missing Features – Part 2

I then realized two features that I love about the Expression Encoder templates: AutoLoad and ThumbnailImage.  These two features are pretty much essential for a bandwidth saving playback experience.  AutoLoad basically disables the media from starting to be fetched until the user clicks play.  The ThumbnailImage enables a static screenshot view to be displayed until a media frame could be captured.  These two features work well together.

The AutoLoad (cueing) was critical for me.  I didn’t want media to start downloading until the user said so.  This saves me bandwidth as well as doesn’t annoy the user if there is a ton of media on one page (which might not be a good UX to begin with, but I digress).

I saw an event PlayControlClicked in the framework that I felt I could tap into.  I figured I’d just wire up to that event and set the MediaElement.Source when the user clicked that.  FAIL.  The problem was that the play control in the current framework isn’t even enabled until the media source is set.  This defeated my whole purpose.

After some spelunking in the source – did I mention that SMF is Open Source? – I found the culprit functions.  Disabling them made everything work but it just didn’t feel right.  Luckily one of the developers of the framework, Kevin from Vertigo, and I start chatting (virtually of course, after all nobody ‘talks’ anymore for real right?).  I told him of my findings and hacks and he educated me that I didn’t even need to mess with the source, but could accomplish my needs by subclassing the Player.  Kevin sent me some sample code for what he called a DeferredSource, which is what I wanted.

After some quick tests, I realized that I should keep all scenarios enabled:

  • Deferred loading (AutoLoad=false)
  • Normal progressive playback (AutoLoad=true)
  • Windows Streaming
  • IIS Smooth Streaming

I modified Kevin’s source a bit and got everything working.  Now I have 3 parameters:

  • media – the URI of the stream, IIS Smooth Streaming manifest, or media file for progressive download
  • autoload – used really only for progressive download, would enable/disable cueing of the video upon load
  • ss – to specify if the URI indicated in ‘media’ is an IIS Smooth Streaming implementation

With this done I can now do something as simple as:

   1: <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="320" height="240">
   2:   <param name="source" value="/ClientBin/SmfSimplePlayer.xap"/>
   3:   <param name="background" value="white" />
   4:   <param name="minRuntimeVersion" value="3.0.40818.0" />
   5:   <param name="initParams" value="media=URL_TO_YOUR_VIDEO" />
   6:   <param name="autoUpgrade" value="true" />
   7:   <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
   8:           <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
   9:   </a>
  10: </object>

Boom, done.  Now I had a player based on SMF that served my needs.

Wishlist

I still didn’t implement the ThumbnailImage in my player.  This is a wishlist item for me…it isn’t critical but nice for when AutoLoad=false so it isn’t just a blank screen!  Additionally, the one thing I have to admit I’m not wild about is the overall size.  The compiled XAP is 230K.  In contrast my SL Video Player is 16K.  Why the big size?  Well, the SMF today is intended for someone who really wants to implement all the features it provides, including Smooth Streaming.  If you aren’t using Smooth Streaming, then you still have those dependencies with you…not ideal.

In talking with the dev team and framework team, I know their plans for updated milestones of SMF and am pleased with the roadmap.  They have taken a lot of feedback of how mainstream uses might be implemented and will make it continue to be awesome with a bit more flexibility of taking what you need!

Summary

If you need a solid, basic player take a look at SMF.  There are other players out there of course, but this one is based on proven best practices in the toughest situations.  And it is only getting better.  There is a lot of room for improvement for the ‘YouTube’ style simplicity of playback for medium-low quality video playback for your personal sites showing home movies, etc. – and I know that scenario will improve, because I’m pushing for it as well.

If you want to use what I’ve done here, feel free – here are the files:

There are also a bunch of videos for working with the Silverlight Media Framework beyond the basics.  Be sure to check them out!

Hope this helps!



This work is licensed under a Creative Commons Attribution By license.

  1. 2/10/2010 11:22 AM | # re: Using Silverlight Media Framework for simple playback
    Great to see you are driving stuff like this! We need more "last mile" efforts to get ready-to-use components in people's hands.

    If I may add to your wishlist, it would be great to see this componentized so you could dial in the amount of functionality you needed. Would like more middle ground between 16K and 230K.

    Also I would like to see more love put towards audio players. As you know I do music stuff on the side... I could really use a nice audio player that handles playlists well. I've used the SL Audio Player out on CodePlex but it's still kinda hokey (can't just hit the play button to start the list; doesn't transition between songs, etc) I know video is the new hotness and all that, but audio is still very important too.

    Would be really awesome to have an audio player that was also a slide show of images... okay, I'll shut up now.
  2. 2/10/2010 11:25 AM | # re: Using Silverlight Media Framework for simple playback
    Jason -- yeah, I'm thinking a plugin model for dialing in what you need/want is ideal ;-) ;-)

    For the audio player -- agreed. What's the best UI one you've seen out there that meets your needs?
  3. 2/10/2010 12:14 PM | # re: Using Silverlight Media Framework for simple playback
    I get "Error 1 The tag 'CoreSmoothStreamingMediaElement' does not exist in XML namespace 'clr-namespace:Microsoft.SilverlightMediaFramework.Player;assembly=Microsoft.SilverlightMediaFramework.Player'. C:\Project\SilverLight\SmfSimplePlayer\MainPage.xaml 12 14 SmfSimplePlayer

    Compiling this project (same for all Smooth Streaming sample projects). I've noticed quite a few others posting the same complaint. I have latest IIS Smooth Streaming SDK and Silverlight Media Framework components downloaded, this is what I have:

    // Assembly Microsoft.Web.Media.SmoothStreaming, Version 3.0.711.8


    Location: C:\Project\SilverLight\IISSmoothSDK\Microsoft.Web.Media.SmoothStreaming.dll
    Name: Microsoft.Web.Media.SmoothStreaming, Version=3.0.711.8, Culture=neutral, PublicKeyToken=31bf3856ad364e35
    Type: Library

    // Assembly Microsoft.SilverlightMediaFramework.Player, Version 1.2009.1112.1


    Location: C:\Project\SilverLight\SMF\Microsoft.SilverlightMediaFramework.Player.dll
    Name: Microsoft.SilverlightMediaFramework.Player, Version=1.2009.1112.1, Culture=neutral, PublicKeyToken=null
    Type: Library
  4. 2/10/2010 12:43 PM | # re: Using Silverlight Media Framework for simple playback
    Matt - Make sure you get the latest SMF..here's the refs I have:
    Microsoft.SilverlightMediaFramework.* 1.2010.114.1
    Microsoft.Web.Media.SmoothStreaming 30.0.711.8
  5. 2/10/2010 3:00 PM | # re: Using Silverlight Media Framework for simple playback
    Cool, I'll have to try it when i get time. Heh, when I get time.. I'll have to make time after I code that Ruby thing for Scott.
  6. 2/10/2010 10:38 PM | # re: Using Silverlight Media Framework for simple playback
    the link to the compiled xap isn't working
  7. 2/11/2010 12:25 AM | # re: Using Silverlight Media Framework for simple playback
    Hi,

    Awsome article. This kind of the information is the I am Looking for.

    Thanks,
    Thanigainathan.S
  8. 2/11/2010 6:08 AM | # re: Using Silverlight Media Framework for simple playback
    i'm used to working with the native media element for audio playback. i thought the smf would be very helpful for a new dj show we were putting together. the concept is to have a guest dj spin a music compilation promoting new releases available from our website. i created a silverlight project, added the smf, created a custom marker class to add track title, artist, and image, then added the markers for each song in the mix, wired up the marker reached/marker clicked event, and plugged in our first dj set from denmark, everything worked pretty good. as songs played the album art would change based on a marker event. and the audiance could click on the marker to advance to the specific track. both of these features are rarely seen and the dj market segment is going to go bananas over this feature. after about 6 hours, i had a working demo. however, once the markers were clicked the track timing eventually went out of whack. i was pleased at how easy this was to put together using the smf and i'm pretty sure the way i implmented is very suspect. the advanced features are very promising and look forward to future progress. thanks for the post.
  9. 2/11/2010 7:26 AM | # re: Using Silverlight Media Framework for simple playback
    Stuart -- my bad, corrected.
  10. 2/12/2010 6:20 AM | # re: Using Silverlight Media Framework for simple playback
    The smooth streaming dll has a big impact on the xap size. It makes up for half the size. So even if the SMF was stripped down to a minimum, the size wouldn't go below 100 K. May we hope for some of the core smooth streaming functionality to be built in to Silverlight 5?
  11. 2/12/2010 7:12 AM | # re: Using Silverlight Media Framework for simple playback
    Tor -- that's right. As I said above that's my only complaint. If you aren't using Smooth Streaming, you still bring it along with you (the Smooth Streaming Media Element). This is planned to change in the next iteration of SMF...more like a cafeteria style -- take what you need.
  12. 2/12/2010 1:52 PM | # re: Using Silverlight Media Framework for simple playback
    For some scenarios you may want to start the video at a specific time. The only way currently to do that is to hook up the MediaOpened event on your CoreSmoothStreamingMediaElement object. In that event you can then set the Position property on the CoreSmoothStreamingMediaElement. It has to open the media first in order to set a starting position. I hope this information saves someone a headache.
  13. 3/2/2010 3:55 AM | # re: Using Silverlight Media Framework for simple playback
    Hi;
    It is Great article. very informative. I run your source code on my system. It compile successfully but when debug it give me exception. The detail of exception is given bellow.

    System.Collections.Generic.KeyNotFoundException was unhandled by user code
    Message="The given key was not present in the dictionary."
    StackTrace:
    at System.ThrowHelper.ThrowKeyNotFoundException()
    at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    at SmfSimplePlayer.App.Application_Startup(Object sender, StartupEventArgs e)
    at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
    at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
    InnerException:

    Please Help
  14. 3/2/2010 7:24 AM | # re: Using Silverlight Media Framework for simple playback
    Waseem -- you are missing one of the startup parameters.
  15. 4/6/2010 1:31 PM | # re: Using Silverlight Media Framework for simple playback
    Tim, can you elaborate on how to use the Media initparams please? I removed the SmoothStreamingSource property from the XAML player, and then tried to pass that through the media param, but it didnt work. I am also curious how to dynamically set this initparam from my asp.net page as well.
    Thanks
  16. 4/6/2010 2:57 PM | # re: Using Silverlight Media Framework for simple playback
    I wanted to elaborate on what I said above...basically, I made all the changes in the code, added the customsmf class etc. I then tried setting the media param to the sample video url that smf provides, I even tried a url to a wmv file on the web and also a ~/Video/myVideo.wmv param for a local video. Couldnt get any video to play in all the cases. I am using the 1/14/2010 dll's.
  17. 4/6/2010 3:11 PM | # re: Using Silverlight Media Framework for simple playback
    Reza -- look at the sample above for setting the media={} param in the object tag
  18. 4/6/2010 4:40 PM | # re: Using Silverlight Media Framework for simple playback
    Tim..found the problem. I had to host my web project in local IIS to get a URI instead of using ~/Video/myVideo.wmv'

    I did some more research and I found that the newest SMF dll has progressive download built in. So in the player, instead of using StreamingSource, you can just use the Source property and link back to the wmv file. This eliminates for some of your custom code. As far as the mediaSource is the initparams, that can be reused.
  19. 4/6/2010 5:15 PM | # re: Using Silverlight Media Framework for simple playback
    Reza -- correct, my sample above uses the new SMF with progressive download support. My 'custom' fork was to support lazy loading of the media instead of starting downloading before the user even hits play (potentially wasting bandwidth)
  20. 6/6/2010 11:32 PM | # re: Using Silverlight Media Framework for simple playback
    Tim-when you extended the code with the _cue variable, is that strictly only to download the file after the play button is clicked when _cue is false?
    Also..the codeplex site says that this player is capable of slow motion playing, are you aware on how to add that button onto the player?
    Thanks
  21. 6/14/2010 6:53 PM | # re: Using Silverlight Media Framework for simple playback
    Tim, will you be following this up with the new SMF v2 player? Looks like everything is different in the new version.
    Thanks
  22. 6/15/2010 2:50 AM | # re: Using Silverlight Media Framework for simple playback
    Reza - the v2 framework has this capability taken into account and they will provide a compiled XAP as well, so there would be no need to redo this for v2
  23. 7/8/2010 5:29 AM | # re: Using Silverlight Media Framework for simple playback
    Teşekkürler güzel bilgiler, elinize sağlık ;)
  24. 7/13/2010 10:45 AM | # re: Using Silverlight Media Framework for simple playback
    I hope that the above code is open source.
    I used it in my project
  25. 7/14/2010 4:49 AM | # 
    Bilgiler için teşekkürler.. ;)
  26. 7/17/2010 3:59 PM | # re: Using Silverlight Media Framework for simple playback
    I was looking for some useful posts on Media Framework of Silverlight. I am happy to find this post, now I can easily use it. Thanks
  27. 7/22/2010 8:02 AM | # re: Using Silverlight Media Framework for simple playback
    this is good article thanks so much
  28. 7/22/2010 8:04 AM | # re: Using Silverlight Media Framework for simple playback
    really thank you man
  29. 7/22/2010 8:06 AM | # re: Using Silverlight Media Framework for simple playback
    so good article in here :) thanks for all
  30. 7/22/2010 8:09 AM | # re: Using Silverlight Media Framework for simple playback
    i liked this article thanks :)
  31. 7/22/2010 8:12 AM | # re: Using Silverlight Media Framework for simple playback
    wonderful article its really good thanks
  32. 7/23/2010 9:08 AM | # 
    this is good article thanks so much
  33. 7/25/2010 12:32 PM | # re: Using Silverlight Media Framework for simple playback
    very gods thank you
  34. 7/28/2010 4:00 AM | # re: Using Silverlight Media Framework for simple playback
    thanks very good share..

 
Please add 5 and 8 and type the answer here:
First time here? You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting! (hide this)