Friday, January 23, 2009

What I Learned Today - How To Play Multiple Media Files In Silverlight

So I am trying to write a learning game for children in Silverlight and I wanted to prompt the user with two concatenated MP3 audio files, so file 1 would play: "Can you find the letter" and file 2 would complete the request by playing: "A" or "B". So the full sentence should sound something like "Can you find the letter A?". Silverlight kept playing both files at the same time, or it wouldn't play them at all, or it would play one of them the first time and the other one the second time, it was very weird. And because I wanted to selected the file that I wanted each time the user progressed in the game I didn't want to have to specify each of my sound files in the XAML, I wanted to be able to do it all programatically, but all of the examples I found were for defining multiple MediaElement objects in the XAML and then calling them from the code behind.

So what I ended up finally doing was specifying two MediaElements in my XAML like so:

<mediaelement name="StaticMediaElement" autoplay="False" />
<mediaelement name="DynamicMediaElement" autoplay="False" />

Then in my code behind I had to do a little fenagling but it was worth it because it worked beautifully:

private void InitializeGame()
{
StaticMediaElement.MediaOpened += new RoutedEventHandler(StaticMediaElement_MediaOpened);
StaticMediaElement.MediaEnded += new RoutedEventHandler(StaticMediaElement_MediaEnded);
}

private void PromptForLetter()
{
DynamicMediaElement.Source = new Uri(String.Format("/Sounds/Letter{0}.mp3", selectedLetter), UriKind.Relative);
StaticMediaElement.Source = new Uri("/Sounds/CanYouFindTheLetter1.mp3", UriKind.Relative);
}

void
StaticMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
((MediaElement)sender).Play();
}
void
StaticMediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
DynamicMediaElement.Play();
StaticMediaElement.Stop();
}


So what this does is it loads up the static media file and waits for it to finish loading before it tries to play it, and while it's doing that it also loads up the dynamic media file and as soon as the MediaEnded event fires for the static file it kicks off the dynamic file. This allows the two sound files to play one right after the other with very little delay in between them.

I was really banging my head against the wall trying to figure this out and that's what I learned today.

Thursday, January 22, 2009

What I Learned Today - Mono is getting to be faster then .NET in some areas

Today I learned that Mono might be worth taking another look at, as it is proving to be (at least in some cases) faster then the Microsoft .NET runtime. This is all according to this article, which is alleging that Mono may be overtaking .NET in some critical categories. So I've downloaded Mono and I'm going to see how well it plays with some of the chunks of code I've written for other projects.

I also learned how to include audio and video in Silverlight, by using the MediaElement object, but I have been having a heck of a time getting it to play nice when I add the media elements programatically, instead of coding them right into the XML. Still wrestling with that one, so it's not my official WILT.

Wednesday, January 21, 2009

What I Learned Today - Hello World in Silverlight

I have decided that I need to learn something new everyday in order to continue to progress as a developer and as a person. I will begin blogging about the new things that I am learning on a daily basis to track my progression and to act as a learning journal of sorts.

What I Learned Today: How to write a Hello World app for Silverlight.

I decided that I wanted to at least get a cursory introduction to Microsoft Silverlight Development, so I have started going through ScottGu's multipart Silverlight tutorial (which can be found here).

Overall Silverlight allows me to develop in the C# programming language using Visual Studio 2008 (which is a pretty slick development environment), so I was able to jump in with both feet and really start digging around. So far I have a Silverlight app with a button and a textblock and when I click on the button the textblock says "Hello World!". It's a first (lame) step, and I don't really know what I want to build as my first real project, so we'll see where it leads.

Well, there's my first What I Learned Today post. I have to say it feels good.