The first way is the inbuilt function, what's very negative with this way is that it could only play .wav files. To play a .wav file from your computer you simply uses it path together with the function My.Computer.Audio.Play(), like this:
My.Computer.Audio.Play(musicPath)
To stop it you just use:
My.Computer.Audio.Stop
and here you don't need to specify any parameters since you can only play one sound at the time.
By default the sound will be played in the background but you can also set how it will be played, in the background, in the background looping until it stops by My.Computer.Audio.Stop or wait to complete which will pause the calling code until the sound has finished.
My.Computer.Audio.Play(musicPath, AudioPlayMode.Background)
My.Computer.Audio.Play(musicPath, AudioPlayMode.BackgroundLoop)
My.Computer.Audio.Play(musicPath, AudioPlayMode.WaitToComplete)
The other way to play sounds is much more advanced, it can play a lot different sound formats(I don't have a list of which ones) and can also play more then one sound at the time. To do all this you have to declare this function:
Public 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
Now you'll be able to use all its functionalities by using mciSendString.
To make everything simpler we'll declare to variables to store some info in, musicPath and musicAlias:
Dim musicAlias As String = "myAudio"
Dim musicPath As String = "C:\Users\Public\Music\Sample Music\Symphony_No_3.wma"
musicAlias is which name we'll give our sound, by giving different sounds different names we can control them one by one. musicPath is where we have the sound on the computer, her I'll just use some Sample music found on my computer. To "create" a sound we will Open it from the file and giving it an alias which will be used when we're doing anything with it, so to open the file at the path saved in musicPath with the name stored in the variable musicAlias we're doing like this:
mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)
So now we will have an audio called myAudio since that was what we have stored in musicAlias. Now we can use this name to control the sound. To start playing it we'll use:
mciSendString("play " & musicAlias, CStr(0), 0, 0)
And since everything now is working in the same way I don't think I need to explain it, just look at the commented code below:
'Start playing the sound
mciSendString("play " & musicAlias, CStr(0), 0, 0)
'Pauses the sound
mciSendString("pause " & musicAlias, CStr(0), 0, 0)
'Resumes the paused sound
mciSendString("resume " & musicAlias, CStr(0), 0, 0)
'Stop the playing sound
mciSendString("stop " & musicAlias, CStr(0), 0, 0)
'Close the sound, this is the opposite to Open and will make so
'you can't use the sound any more'unless you open it again of course.
mciSendString("close " & musicAlias, CStr(0), 0, 0)
So if you now want to play two sounds simultaneous you just give them other aliases(for example myAudio1 and myAudio2) or if you want to have a background music playing and playing while sound effects are played from time to time you just do the same(you can name one myBackgroundMusic which you play all the time, then you can name other ones like new, prompt, welcome and so on and play them while needed).
And that was pretty much it, if you think I've forgotten anything just leave a comment below. Bye
