XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

   Playing audio has certainly got a lot easier now that the 2005 version includes the very useful My.Computer.Audio.Play method.   

 

  If you know where the audio file is going to be saved then you can use something like:

 

   My.Computer.Audio.Play(Application.StartupPath & "\DingDong.wav")

Or

   My.Computer.Audio.Play("C:\Start.Wav")

 

   Of course, there is always the danger that the audio file won’t be where you (or worse yet, the user) expects it to be.   So you may want to let them choose for themselves:

 

  Dim retVal As DialogResult

  Dim ofd As New OpenFileDialog

  ofd.Filter = "Wav Files|*.wav;"

  ofd.RestoreDirectory = False ' Re-open last used folder

  retVal = ofd.ShowDialog

  If retVal = DialogResult.OK Then My.Computer.Audio.Play(ofd.FileName)

 

   Chances are that this won’t be a realistic option in many situations, though.

 

      So maybe we should make use of the project’s Resources, as these are  now much easier to use in VS 2005 ?   Not tried that yet?   OK, here’s how to  add an Audio Resource:

 

  

  1. Right click on the Project name in Solution Explorer and Select ‘Properties’
  2. Select ‘Resources’ from the left hand side of the page which is then displayed.
  3. Select ‘Audio’ from the first of the horizontal tabs (it may be showing its default choice of ‘String’.) 
  4. Click on the ‘Add Resource’ button, which is located next to the tab you have just used.
  5. From the File Browser dialog, navigate to your chosen Wav file and click OK.

   That’s it.  You can double-click on it to hear it play, just to be sure.  It will now be compiled in with your application when you build the final version for deployment.   No more worrying that you have got all the dependencies lined up and that they are all where they should be.  

 

   Job done, then?   Well, not quite.  You still need to point to this Resource when you want the chosen sound to play.    Based on the samples above, you would probably expect the following code to run just fine:

 

  My.Computer.Audio.Play(My.Resources.Boo)

 

   (where ‘Boo’ is the name of the Wav file I saved as a resource.)

 

       But as you will see if you try it, you will get a compiler warning that tells you that the particular resource cannot be converted to the required String Type.   When I first hit this problem, I spent (wasted) quite a lot of time trying to find a variation of Type casting that would accept the Resource in this way.

 

      Eventually, I discovered that the solution to the problem was to use one of the overloaded versions of the Audio.Play method.   Specifically, the one which takes a Stream as the first parameter and one of the AudioPlay Enumeration members as the second parameter.

 

i.e.

      My.Computer.Audio.Play(My.Resources.Boo, AudioPlayMode.Background)

 

   Now it will happily play your Resource sound for you.   The reason that this works is that this particular overloaded version happily takes the Resource, which is now a Stream ; whereas the version with the single parameter required a String that pointed to a physical file location.

 

   Another little gotcha that will getcha if you’re not aware of it.

 

 

posted on Friday, February 03, 2006 9:49 AM