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.

Tuesday, March 18, 2008

Do Good Practices Take The Fun Out Of Learning?

As I progress in my endeavor to learn Python I am faced with several challenges, foremost among them being a lack of time to devote to the task, shoot I've been walking through a Django tutorial for over a week now and I'm only halfway through! Another roadblock I am faced with is the fact that I am working with C# all day in an enterprise environment, so I can hardly sit down to write a simple Python script without the intense urge to first setup a multi tiered project for it and check it into source control. Once I do get my multi tiered project created, unit tests written (or at least started), and everything checked in, I usually "take a break" for a day or two before I sit down to the script again.

If I were a newbie to programming in general I think that Python would be much easier (and possibly more fun) to learn, because I wouldn't have the overhead of trying to do things "right", instead I would be focusing on just trying to get whatever script I have decided to write working the way I want it to. But knowledge once obtained cannot be unlearned, and as I have become more and more familiar with good design practices and patterns, I have seen many of the advantages to implementing them and for all of it's slowness, I think it's best to force myself to port my existing knowledge of good programming practices to any new languages that I learn.

I must say that in spite of the delays, I am still enjoying learning a new language and joining the culture behind it, in fact I am starting to feel like some sort of trend setter among some of my peers, mostly .NET die hards, who don't seem to be as open as I am to broadening their programming horizons. Hopefully I can convince them that this isn't just a fad for me, but that I'm in fact committed to becoming truly adapt and closely familiar with Python.

Wednesday, March 12, 2008

Why Your Sex Life Sucks

When my wife and I got engaged, people said "There's no sex after marriage", which concerned me quite a bit since we weren't having sex before marriage, so if there wasn't going to be any after marriage then we were really in trouble. After a couple of years, when we got pregnant with our daughter people said "There's no sex after you have kids", which also concerned us because we were thus far enjoying a very lucrative sex life. Then after 3 or 4 years of being married people said "The 7 year itch has turned into the 5 year itch, expect your sex life to decrease greatly", which sucks because, well we had no way to refute these claims except to wait and see, and of course try to do it like bunnies, you know, stocking up for the winter(?).

Well, we've been married for 5 years, have two beautiful children, and I'll be honest with you, our sex life is (almost) better now then it has ever been. I tag on that "almost" because we are so busy right now with me finishing up school, working full time, my wife taking ballet classes 2 to 3 times a week, and with the kids, that we're often quite exhausted by the time we climb into bed. But even with that exhaustion, we still enjoy a very healthy and vigorous sex life, the likes of which are apparently unheard of within my wife's circle of friends.

I refer to the circle of friends my wife goes walking with at the mall with, several other young mothers in situations similar to ours (working husband, stay at home or part time working mom, children). Apparently the number of times that we have sex and the level of enjoyment that we get out of it seem to far surpass the other young couples that we know. And while they try to use excuses ("you didn't do it before you got married, so once you've actually been doing it as long as we have you won't want it anymore" is a popular one), I think that I have found at least one probable cause for the decline in sex after marriage, children, and years of living together, and I'm not placing blame where you might expect me to. That's right fella's, I'm looking at you.

So you get married, you get comfortable, you put on some weight, and the sex declines. Maybe your wife puts on some weight too, my poor wife did after we got married and she started eating like her pig of a husband. That just means that not only does she think that you're fat, but she doesn't feel beautiful anymore and really doesn't want to do it.

Then your wife gets pregnant and (in most cases) you put on some pregnancy weight of your own, and...that's right, your sex life declines. Can you blame your wife for not wanting your big fat self climbing all over her big pregnant self? I wouldn't blame her one bit.

Then you've been married for 5 or 6 years, and with the kids, the job, those summer BBQ's, and good ol' home cooking, again (statistics show) you put on some more weight, and sex declines.

Now I hate to ask an uncomfortable question here, but look at yourself in the mirror and ask yourself "if I were a girl (or a gay guy), would I want to have sex with me?". Now I'm not passing judgment here, only asking you to pass it on yourself, but my guess is that an honest answer will be a resounding "hell no". Your wife may be too nice to admit it to you, or perhaps she's too sweet to even realize why she would rather roll over and go to sleep then try to wrestle with you and the two other people that you ate. Either way, I have a little experiment that will help you determine if your weight is (at least part of) the reason that your sex life has slowed down.

The first part of my experiment is this, get on a diet that you can stick with to get and keep the weight off. I strongly suggest Weight Watchers, but there are some other very effective ones out there (Slim For Life is supposed to be very good as well). One nice thing about Weight Watchers is that you don't have to go to the meetings, you can join online for around $12d a month and use their eTools to track your weight loss and points usage. Whatever diet you choose, try to find one that doesn't make you feel like you're dieting. You know, that deprived feeling like you're always missing out on something.

So with the weight coming off, now we are going to up the anti a little bit with the second part of my experiment by putting on some muscle. Go walking or jogging (getting a running buddy really helps), get some hand weights from Wal-Mart and lift those two or three times a week, get a gym membership and actually go regularly.

Finally, with the weight coming off and at least a little muscle hopefully going on, now it's time to see if our experiment was successful. If your wife starts wanting to have sex with you more, then I think we can say with some level of totally unscientific certainty that you being fat was the reason that your sex life sucked.

All I'm going to say is that my wife thought I was hot before I dropped 40 pounds and started working out for a mere 30 minutes three times a week, but now she literally can't keep her hands off of me and I practically have to threaten to go sleep on the couch if she doesn't let me get some rest at night. It's pretty much awesome! I'd strongly suggest following through with my above outlined experiment, it could end up being a good thing for you, and your wife might appreciate it too.

Thursday, March 6, 2008

A Blog With A Short Attention Span

Apparently blogging is the thing to be doing these days, so here's my own little addition to the blogging world. I'm not sure what I'll be talking about here, but as the name of the blog and the title of this post indicate it will probably be on a myriad of topics ranging from programming to GNU/Linux, maybe some family stuff, we'll see.

I actually started writing my own blogging software (you know, as a learning opportunity) but I thought it best to just get this thing up and running sooner then later. I was planning on hosting it on my own server at home but I was concerned that it might end up eating up enough upstream bandwidth to cause problems for the services I have running from home. But the main reason for wanting to get this blog off the ground right away is that there is a lot going on in my life that I want to talk about right now and I don't want to miss anything. For example, I'm teaching myself Python, so I have a lot to talk about as I make progress there. Also, we're trying to refine our SCRUM methodology at work and I need an outlet for the frustration and concerns that this process has incurred. Mostly, I just want to have a voice in this great big internet, so here it is.