Sunday, January 12, 2014

Better way to play sound file on WPF button click

Recently I was working on a XAML based application, in which my requirement was to play a sound (.wav file) whenever a given button is clicked. So, to achieve this, I wrote a below snippet:










My above snippet worked but at the same time, I feel a noticeable delay in beep sound and that make me analyze further and write a blog post :)

Now question is, why there is delay between button click and sound ???
Well, there is a simple concept behind it, which I missed while implementing above requirement :(

Reason is, the event hierarchy. 

Most of us might be aware that Click is a bubbling event, which means event will be fired from the control who initiated it. So, in our case, whenever button is clicked, it bubbles from button to window. And that's the reason, click event handler is executing before the window event is triggered, which is ultimately leading to delay.

Now, how to handle this???

Method 1:
Then I thought to write a preview event, which will be fired before my button click and came out with this below code:











In the above snippet, I am using VisualTreeHelper to verify is click event is occurred on button or not. Don't you think, it is bit dreadful? If it seems good, then you can go ahead with this approach else I got one more idea to implement this using ResourceDictionary.

Method 2:
Let's use Styles and EventSetter to achieve the same goal. Here I am going to define my style in a ResourceDictionary, now of course I don't have instance that I could wire my event to. For this, I created a code-behind file for ResourceDictionary, which will appear as a child of my XAML file and will contain the below code:












Now only thing pending is merging the ResourceDictionary with our MainWindow, which can be done as:










For me, this approach looks better. Hope you enjoyed reading!!!

7 comments:

  1. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. Soundcloud to mp3

    ReplyDelete
  2. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. piano

    ReplyDelete
  3. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. youtube to mp3

    ReplyDelete
  4. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. acim soundcloud david hoffmeister

    ReplyDelete
  5. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. snaptube mp3

    ReplyDelete