Monday, January 19, 2009

Dynamic Loading Expression Media Player Template and passing parameters by code

Following my previous post on How to pass parameters to the Expression MediaPlayer component by code, here is a solution that builds upon this post to Dynamicaly load the Media Player inside your app and pass parameters to it.

You can test loading of the player here. The source code can also be downloaded here.

The loading and costumization of the parameters is done like this:

// Download the Media Player using WebClient
private void downloadVideoPlayer()
{
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(onDownloadVideoPlayerCompleted);
downloader.OpenReadAsync(new Uri("MediaPlayerTemplate.xap", UriKind.Relative));
lblLoadPlayer.Text = "Downloading Media Player";
}

// Once the Media Player is downloaded
private void onDownloadVideoPlayerCompleted(object sender, OpenReadCompletedEventArgs args)
{
try
{
string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(args.Result, null), new Uri("AppManifest.xaml",UriKind.Relative)).Stream).ReadToEnd();

XElement deploymentRoot = XDocument.Parse(appManifest).Root;
List<XElement> deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements()
select assemblyParts).ToList();

Assembly asm = null;
foreach (XElement xElement in deploymentParts)
{
string source = xElement.Attribute("Source").Value;
AssemblyPart asmPart = new AssemblyPart();
StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(args.Result, "application/binary"), new Uri(source, UriKind.Relative));
if (source == "MediaPlayerTemplate.dll")
{
asm = asmPart.Load(streamInfo.Stream);
}
else asmPart.Load(streamInfo.Stream);
}



MediaPlayerTemplate.Page myPlayer = asm.CreateInstance("MediaPlayerTemplate.Page") as MediaPlayerTemplate.Page;
Dictionary<string,string> dic = new Dictionary<string, string>();
dic.Add("autoplay", "true");
dic.Add("enablecaptions", "true");
dic.Add("muted", "false");
dic.Add("stretchmode", "0");
dic.Add("displaytimecode", "false");
dic.Add("playlist", "<playList><playListItems><playListItem title=\"\" description=\"\" mediaSource=\"silverlight.wmv\" adaptiveStreaming=\"False\" thumbSource=\"\" frameRate=\"23.9760431376968\" width=\"512\" height=\"284\" ><chapters><chapter position=\"11.256\" title=\"MYMARKER01\" /><chapter position=\"20.033\" thumbnailSource=\"silverlight_20.033.jpg\" title=\"Capitulo%201\" /><chapter position=\"45.585\" thumbnailSource=\"silverlight_45.585.jpg\" title=\"Chapter%202\" /><chapter position=\"58.646\" thumbnailSource=\"silverlight_58.646.jpg\" title=\"Chapter%203\" /><chapter position=\"72.199\" thumbnailSource=\"silverlight_72.199.jpg\" title=\"Chapter%204\" /></chapters></playListItem></playListItems></playList>");
myPlayer.StartUp(dic);

cnvMediaPlayer.Children.Add(myPlayer);
lblLoadPlayer.Text = "";
LayoutRoot.UpdateLayout();

}
catch (Exception e)
{
lblLoadPlayer.Text = "Download Error: " + e.Message;
}
}



If you download the source code you will need to create your own playlist that points to a a movie of your own and set the thumbnails and chapters as you wish. Check the following line of code inside VideoPlayerHoster, Page.xaml.cs



dic.Add("playlist", "<playList>...



The Dynamic loading was implemented by looking at these 2 posts:



http://www.silverlighthack.com/post/2008/09/29/Silverlight-2-(RC0-RTM)-Dynamic-Assembly-Loading.aspx



http://silverlight.net/learn/learnvideo.aspx?video=65687



11 comments:

Unknown said...

Hi Tiago. Great Apps. Im trying open them using VS2008 and Expression Blend, but i keep getting the errors "Project type not supported by this installation". Any suggestions?

Tiago Andrade e Silva said...

Hi!
Do you have all it is needed to develop silverlight 2 apps ?

Take a look at http://silverlight.net/GetStarted/

This solutions was developed in VS2008 so you should not have a problem if you have all the needed components installed.

bye,
Tiago Andrade e Silva

Unknown said...

Thanks. I had to refresh the tools from the VS Command prompt.

I am interested in the funcionality of the List on the right of the viewer (see image:
http://i303.photobucket.com/albums/nn121/kaanuki/player_chapters.png)

What is it and what does it do? can you provide some sample code of its funtions?

Unknown said...
This comment has been removed by the author.
Tiago Andrade e Silva said...

Hi!
What information comes directly from the wmv file. You can add some metadata to the wmv and the player will strip the information.

There is also some event OnMarkerReached (I think that is the name).

The wmv was created using Expression encoder. You can follow some Starter Guide at:
http://expression.microsoft.com/en-us/cc184877.aspx

Unknown said...

http://s303.photobucket.com/albums/nn121/kaanuki/?action=view&current=player_chapters.png

Unknown said...

thanks for the prompt responses!

Tiago Andrade e Silva said...

I happen to be online :)

Anonymous said...

Hi Tiago,

I have question about SmoothHD streaming. When I try to stream I get the following error: "Requires ... template that supports smooth streaming"

How do I change the template?

thanks in advance,

Omid

Tiago Andrade e Silva said...

Hi!
Are you using windows encoder 2 ? try the latest version 3 :)

Unknown said...
This comment has been removed by a blog administrator.