How to change Video Source?

Dec 4, 2010 at 6:57 PM


I'd like to have a list of videos and play the video that the user selects.  I'm having trouble getting this to work.  So far I'm going this in the code behind when they select a video:

PlaylistItem video = new PlaylistItem();
video.DeliveryMethod = Microsoft.SilverlightMediaFramework.Plugins.Primitives.

video.StartPosition =

video.MediaSource =

new Uri( "http://locahost/SilverlightVideos/video2.wmv" );
this.MediaPlayer.Playlist.Add( video );
this.MediaPlayer.GoToPlaylistItem( 0 );
this.MediaPlayer.CurrentPlaylistItem = video;

Any ideas?

Thanks in advance.

Dec 7, 2010 at 8:03 PM

First, if you go to http://locahost/SilverlightVideos/video2.wmv does it play your video? If not, code might not be the problem here.

Is your list of available videos already available to you on the Silverlight side? If so, perhaps you should load your MediaPlayer.Playlist with the videos that are available. Then on selection change you would only need to do:

MediaPlayer.CurrentPlaylistItem = SomeMethodToGetPlaylistItemByMediaSource(mediaSource);

Then you can implement that method however you want. Perhaps like this:

private PlaylistItem SomeMethodToGetPlaylistItemByMediaSource(Uri mediaSource)
    return MediaPlayer.PlaylistItems.Where(p => p.MediaSource == mediaSource).SingleOrDefault();

That's close to how I do it and it works just fine.

Alternatively, if you already have the playlist loaded with all your available videos you don't need to write special code to switch videos. the users could use the existing playlist control to select a video.

Dec 7, 2010 at 8:26 PM

Hi, thanks for replying.

Yes, the URL to my video(s) work.

I currently have it working by listing all the videos in the <SMFPlayer.Playlist> tag, and then using the built in playlist to switch videos.  This is working, however what I'm really wanting to accomplish is to dynamically load the video, because my list of videos will be changing every day.  Ideally I would like to store the video URLs in an external file (XML maybe?) and build my playlist from that.

Any links or suggestions on how I can do this?

Also, I notice that when I click the videos in the playlist there is sometimes a delay while the video loads.  I'd like to show a loading or buffering animation/message during this time.  Is this possible?  Is this delay normal?


Dec 7, 2010 at 8:46 PM

Now you're getting into an entirely different problem. One that I have recently tackled but I haven't blogged about it yet so I don't have a whole lot of information to tell you. I store information about videos in a SQL Server database. I have a large number of available videos that get loaded at various times in my application. To do this I am using WCF RIA Services. The idea is that I can make a call to a WCF Service to return me a list of my videos. Then my Silverlight assembly can process those videos into PlaylistItems that get fed to the SMF player. If you wish to peruse the WCF RIA Services route I can give you the tutorial that I started with. It is rather challenging to completely understand but if you finish it you will have a solid base for creating your own service to give you a video list from a database. I hope you have luck with it. It is a fantastic solution once you get it working.

As for the delay: If you are doing progressive download it is normal. It is loading the physical file not to be confused with buffering. This behavior can actually vary depending on what browser you are using. For example, Google Chrome may require the entire video to be downloaded (in a progressive environment) before SMF will play the file. Whereas Internet Explorer may issue a 206 and be able to "load" the video immediately and download while you watch. I don't really have a clean solution to letting the user know a video is loading, but I do show a loading control. I have a control that I drop over-top of the player and bind its Visibility to a property that I use to determine if the video is loading. So, until I get a MediaOpened event on the player I know this video is still loading. It works, but I would rather have a better solution myself.

Dec 8, 2010 at 12:49 PM

Thanks again for your help.  I may look into the tutorial if I have time.  Right now I'm more concerened with the 2nd question.

First, I am not using the Smooth Streaming meth because we are not able to install the reqiured components on our servers right now.  So, I'm just using the standard Silverlight Media Framework player.  My first question is, on each playlist item I can choose the delivery method.  I seem to get the same behavior if I choose progressive download or streaming.  Is this correct?  Is it possible to stream at all without using Smooth Streaming?

I have read about the difference between progressive download and streaming and understand them.  I'm not sure which one we will use, but I know they want to prevent these videos from being redistributed so I'm thinking we will be streaming them.

Second, can you elaborate on how I can create a "loading" screen?  Right now, when my playlist item is selected, it animates.  This animation is interrupted by the loading of the video, which is a poor user experience.  I would like for this animation to complete and then show a loading screen until the video is ready.  The animation of the playlist item is just a simple scale transformation and a textblock that appears saying "Now Playing".  Like I said though, this animation stops until the video is loaded.

If you can help with this I would greatly appreciated it.  Thank you!

Dec 9, 2010 at 2:31 PM

I noticed that you said you can't install components on your servers right now, but I thought I'd also mention that if you can get the IIS Media Services extension installed, it will give you the option to use bitrate throttling for certain based on filetype. This in theory is supposed to help lower the initial buffering time and will get the user seeing video at the beginning sooner.

I installed this on our development server recently and I think it improved the scenario you are talking about. Also, it did not require a reboot... may have required a restart of IIS, but I can't remember.

I got the impression that it would be pretty seemless to install even in a production environment, so you may want to look into it.

This is compatible with what you've already set up... progressive download. The smooth streaming option would require re-encoding your video for multiple qualities.


Jan 27, 2011 at 1:45 PM


If you ever get back to pulling data from a WCF Service like I stated earlier I just had a request from another community member to explain that process in more detail. You can find the response on my blog.

Mar 11, 2011 at 5:34 PM
swhook52 wrote:

As for the delay: If you are doing progressive download it is normal. It is loading the physical file not to be confused with buffering. This behavior can actually vary depending on what browser you are using.

This isn't quite true. When using progressive download the player will be able to start buffering and (once it has buffered enough content) will then be able to start playing, once it has read the "moov atom". The "moov atom" is essentially some meta data that, if it appears at the end of the video file, will result in the entire video being downloaded before it can start playing. This tripped me up, so hopefully it will help someone else. For a more in depth explanation and some solutions, see