Thursday Night

Paul Betts’s personal website / blog / what-have-you

Patching Silverlight Media Framework to work with MP4/WMV files

For a project I’m working on at work, I had a need for an easy to embed video control into my Silverlight UI – previously I had been using some code hacked out of an Expression Encoder template, but it had a few bugs and didn’t look very nice. I was quite happy then, when Microsoft released the Silverlight Media Framework during PDC09. It’s got a nice UI, it’s easily themeable and embeddable, and a fairly complete player framework (supports things like timeline markers, etc).

Much to my dismay however, I discovered that it was only compatible with IIS Smooth Streaming files, not the standard MP4 files that I had been using. However, they also ship the source, so I have a chance to do some hacking and turns out, it was pretty easy to make this work properly.

diff –git a/Microsoft.SilverlightMediaFramework.Player/CoreSmoothStreamingMediaElement.cs b/Microsoft.SilverlightMediaFramework.Player/CoreSmoothStreamingMediaElement.cs
index f642d2c..fadd5e6 100755
— a/Microsoft.SilverlightMediaFramework.Player/CoreSmoothStreamingMediaElement.cs
+++ b/Microsoft.SilverlightMediaFramework.Player/CoreSmoothStreamingMediaElement.cs
@@ -725,7 +725,7 @@ namespace Microsoft.SilverlightMediaFramework.Player
                        maxSelectedStreamBitrate = 0;
                        // get video stream
-                       var streamResult = (from stream in SelectedStreams
+                       var streamResult = (from stream in SelectedStreams ?? Enumerable.Empty< StreamInfo > ()
                                                                where stream.Name == "video"
                                                                select stream).SingleOrDefault();

So, if you don’t grok what this is saying, here’s what it means – download the source, open the Microsoft.SilverlightMediaFramework.Player/CoreSmoothStreamingMediaElement.cs file, then go to line 728 or so, and replace "from stream in SelectedStreams" with "from stream in SelectedStreams ?? Enumerable.Empty<StreamInfo>()". Then, rebuild the project (remember, you have to download Microsoft.Web.Media.SmoothStreaming.dll from here before you’ll be able to build the project.

Once you have the patched SMF, you should be able to play any file by doing something like:

void setPlayerToUrl(string url)
    var cme = new CoreSmoothStreamingMediaElement() {
    Source = new Uri(url),
    Player.MediaElement = cme;

Written by Paul Betts

November 22nd, 2009 at 4:29 pm

Posted in WPF / Silverlight