Tag Archives: .Wav

How to Play an Embedded Resource .Wave Sound in VB.NET 2008, Visual Basic 2010, and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

I’ve recenty been making an example program with Visual Basic.NET 2008 on snapping forms to the edge of the screen. I wanted the program to play a sound wave file when it performed the snapping process. But I didn’t want to have to make it link to external wave files or anything. So I decided to embed the wave sound bytes in the program itself. So this little article will show you how I went about doing that. Here is a link to the actual example application that this article is based on.

Example Playing Embedded Wave Sounds


More Info

The first thing you want to do of course is determine the wave files you want to play in your application. Remember that the size of each .wav file in bytes, willl be added to your programs file size. So remember that when you decide on what sound files you want to use.

I also want to mention that this article is specific to VB .NET 2005/2008/2010 and higher since I am using the SoundPlayer class. Visual Basic .NET 2002/2003 will have to use the PlaySound api thats part of the WinMM.dll library. The PlaySound api can play and do everything the SoundPlayer class in VB 2005 and higher can do. Just more code is involved. I actually found an article after I started this post that shows how to play wave files embedded in your VB.NET 02/03 application. Just click here to check it out.

Embedding your .Wave Files

After you have determined the wave files you want to embed in your program you need to add them to your project. There are a couple ways to do this. The easiest way is to copy your .wav file and then paste it in your project. To paste it to your project you just need to select your project name in the Solution Explorer, usually the top right panel. Then right click and click on the paste command. Another way is to click on the “Project” and click on “Add Existing Item”. Then just browse to the wave files location and select each file you want to embed. Once the files are added to your project, you will want to click on each wave file and in the properties panel (Its right below the Solution Explorer by Default). In the Properties panel you will see “Build Action”. From the build action combo list select “Embedded Resource”. Do that for each sound file you added to your project.

Playing your Embedded Files

Now that you have embedded your files into your application, its time to setup the code to play them. If your using .NET older than 2005 then check out this article on using the unManaged api call “PlaySound” to do the playback.

You now need to access your newly embedded *.wav file. You can access it as a stream under your programs manifest. Here is the method that will be used…

Public Overridable Function GetManifestResourceStream(ByVal name As String) As System.IO.Stream Continue reading

Readymade Options for Playback of Music/Audio Files in VB and VB.NET – 1 of 2

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 3.90 out of 5)
 

This article and Part Two are to show some options available for Audio/Music playback if your not interested in making your own control or library. I do have an article on how to do basic music playback at this link with some quick and simple source code. But if you want more advanced features and don’t want to create the bulk of the playback code, then the content in these articles should help you out. This first article is on using some libraries I made around the MCISendString Windows API function and doesn’t have 3rd party dependency.

Note: You can now check out part two at this link on two 3rd party controls named Bass.dll and FMod.dll.


Visual Basic 6.0

For VB 6.0 is Music Module v2.2 that I made with Visual Basic 6.0 and contains the main features that a music player usually needs. It supports the most popular music formats like Wave, MIDI, and MP3 files. Some other features are calculating the MP3’s overall Bitrate, getting the Playing Position in various formats, Left/Right Volume Control with Muting, etc. While the class module is old, it still does the job well and does not rely on any 3rd Party .DLL’s or Resources. You can download the Class Module with Example at this link.

Visual Basic.NET 2002/2003

This option is basically for VB.NET 2002/2003 and would recommend you use it for those versions only. This library is called csMusicLibrary v1.0 Professional. This class contains more features than the VB 6 library.  The class has all of the features required for a normal music player and some extra features like basic playlist support, setting the music playback rate, setting the playing position and more. IF your interested in this option you can download the compiled class with example at this link.

Visual Basic 2005/2008/2010, and Higher

This next library definitely has the most features out of the options in this article. It is a major overhaul of the original csMusicLibrary. It is csMusicLibrary v3.1 – The Professional Series and made with Visual Basic 2005. It is completely XML commented with explanations about the Subs/Functions and Properties along with remarks. There are many options available and is easy to use and understand. Besides your normal playback features it can give your applications some basic recording capabilities, extract MP3 Tag v1.1 information, get the music playback status, and much more. Like the other options outlined above, this library does not rely on any 3rd party controls or resources. If you don’t need any super advanced features like visualizations, data manipulation, or sound effects, etc. then this library should work just fine for making a fully featured music application. You can download the compiled class with example at this link.


All these options should be fine for making a nice featured application. But you may be wanting more advanced features like FFT’s, Data Manipulation, Sound FX, and so on. That is where the next article will come in. Click this link to read part two about some third party controls. Have fun 🙂

Jason

Revised: 2014

How to Play Wave Files Easily with VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

Playing Wave files with Visual Basic (6.0 and older versions) is quite easy using the Windows APIs. The API you would be interested in using to play wav files is the: PlaySound – API Call. Below is the Constants and Function which should be put in a Module or Declaration section of your project…

   '
    'These Constants and API call was copied from the API-Guide program.

    Private Const SND_APPLICATION = &H80 ' look for application specific association

    Private Const SND_ALIAS = &H10000 ' name is a WIN.INI [sounds] entry

    Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI [sounds] entry identifier

    Private Const SND_ASYNC = &H1 ' play asynchronously

    Private Const SND_FILENAME = &H20000 ' name is a file name

    Private Const SND_LOOP = &H8 ' loop the sound until next PlaySound

    Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file

    Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found

    Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound

    Private Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

    Private Const SND_PURGE = &H40 ' purge non-static events for task

    Private Const SND_RESOURCE = &H40004 ' name is a resource name or atom

    Private Const SND_SYNC = &H0 ' play synchronously (default)

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal _
lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Below is how to use the Playsound function to play a wave file…

'
'This will simply play a wave sound from a file location and aSync so it uses
'a thread other than  your applications thread and will not keep your application
'from responding till its done, like playing Sync would do.
'
'The flag: SND_FILENAME I believe is the default for the PlaySound function.
'You can execute'that flag if you want to make sure it plays the sound file fine.
'
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC)
'
'
'IF you want to use more than 1x flag with the api call, then just call it like below....
'
'This will play a wave file async and loop it over and over till you call the PlaySound
'Function again.
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_LOOP)

And that all there is to it to call and use the Playsound function to play a wave file.

But what if you wanted to stop a wave file that is already playing? That is taken care of below…

'
'This will call the Playsound function to execute and play nothing which will interupt and stop the previous wave file from playing.

PlaySound(vbNullString, 0, 0)

And that is all you have to do to play wav sound files with Visual Basic 6.0. Hope you found this useful 🙂

Jason

 

Minor Revision: 2014

Easily Play, Stop, Pause, and Resume MP3/Wav/WMA files in VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (31 votes, average: 3.42 out of 5)
 

This article will use the already established Windows API function MCISendString. The function is part of the Windows Multi-Media system in the winmm.dll using Multimedia Command Strings (MCI). WinMM is available on pretty much every modern Windows OS. Apart from having to install special codecs for uncommon audio/music formats, you can play many of the popular formats without any special setup or installation. If you want to know more about Multimedia Command strings you can check out a beginner tutorial I made at this link.

While the source code in this post is in VB.NET, you can simply change ‘as Integer‘ to ‘as Long‘. Otherwise the code should be fine.

Below is the API Function to get started.

'
'Api to send the commands to the mci device.
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal _
uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

 

The next snippet is for the path to the file.

'The Chr(34) code is to put quotes at the beginning and end of the file's path.
'You can convert the long filename to the short filename and not need 'the quotes.
'That would just be more code that really isn't needed.
Dim fileToPlay As String

fileToPlay = Chr(34) & ("c:\MyFileToPlay.MP3") & Chr(34)

 

Below is a command sent to the MCI system to open and setup a new device to play the file that is specified in the fileToPlay variable.

'Let the command interface decide which device to use. Just specify the alias.
'The alias is the name you use to program that device. You can create multiple
'devices to play media at the same time and such with different alias's.
MCISendString "open " & fileToPlay & " alias myDevice", vbNullString, 0, 0

 

The device is now setup and you just need to execute the simple commands shown below.
Continue reading