Using Silverlight Media Framework for simple playback| Comments
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.
It has a lot of features built-in to the framework such as:
- DVR-style features
- Fast forward
- Slow motion
- Media Markers
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"/>
Boom, done. Now I had a player based on SMF that served my needs.
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!
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:
- Compiled XAP (Updated 29-APR-2010 for SL4 for full-screen pinning)
- Source code for my modified stand-alone player – note that you will need the SMF and the dependencies for that before this will compile. This source contains only my modifications
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!
Please enjoy some of these other recent posts...