Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Playing sounds in VB.NET

VB.NET form

  • Please log in to reply
20 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 11 November 2009 - 02:52 PM

In this tutorial I will show two ways of playing sounds in VB.NET, the first way is easy to use but not so powerful, the other one could do more things but is a little bit more difficulty to use. Depending on what you need the sound to do you can only pick the one that fits best. If you only need simple stuff use the first one but if it can't do what you want then you just use the other one, as simple as that.


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 :)
  • -2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 12 November 2009 - 06:05 AM

Nicely done. +rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 November 2009 - 08:12 AM

Nice +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 Zerkei

Zerkei

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 26 December 2009 - 10:44 AM

Awesome, i was looking on how to do this a few days ago.
I just ended up only finding the first method, which satisfied me and allowed me to play a 3 second wav file.
+rep (i don't think that will do anything because i have no rep :P)
  • 0

#5 AndrejCro

AndrejCro

    CC Newcomer

  • Just Joined
  • PipPip
  • 23 posts

Posted 11 April 2010 - 01:32 PM

hi!
when i do this to play my file it just makes some weird noise, like when it played the file, it immediately stopped.. so i can hear the actual file but very short, like its been cut out (sorry for my bad explanation)... anyway here is the code, can you check it?

[SIZE=2]musicPath = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"C:\Documents and Settings\Administrator\Desktop\MusicMe\MusicMe\bin\Debug\Drums\kick.wav"[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]musicAlias = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"kick"[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Open "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & Chr(34) & musicPath & Chr(34) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" alias "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & musicAlias, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0)[/SIZE]
[SIZE=2]mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"play "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & musicAlias, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0)[/SIZE]
[SIZE=2]mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"close "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & musicAlias, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0)  'when i delete this last line of code i dont hear nothing[/SIZE]


EDIT:
interesting thing, if i make everything except "close" i can only play it one time!
another thing, if i put in the: Threading.Thread.Sleep(50) then i can hear it but it freezes my program...
  • 0

#6 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 11 April 2010 - 10:30 PM

the reason is the close thing, if you close an audio it closes and can't play anymore, if you want more then one song/sound to be played at once you will have to call them different things, that's the alias part of the code :)
  • 0

#7 AndrejCro

AndrejCro

    CC Newcomer

  • Just Joined
  • PipPip
  • 23 posts

Posted 12 April 2010 - 06:03 AM

hey, thanks for reply.. the thing is, when i dont include the close statement i can play the sound only once then its muted...
if i keep the close statement i can hear the fraction of the sound playing!

so if i leave just this code below:

[SIZE=2]musicPath = [/SIZE][SIZE=2][COLOR=#a31515][COLOR=#a31515]"C:\Documents and Settings\Administrator\Desktop\MusicMe\MusicMe\bin\Debug\Drums\kick.wav"[/COLOR]
[/COLOR]musicAlias = [/SIZE][SIZE=2][COLOR=#a31515][COLOR=#a31515]"kick"[/COLOR]
[/COLOR]mciSendString([COLOR=#a31515][COLOR=#a31515]"Open "[/COLOR][/COLOR] & Chr(34) & musicPath & Chr(34) & [COLOR=#a31515][COLOR=#a31515]" alias "[/COLOR][/COLOR] & musicAlias, [COLOR=#0000ff][COLOR=#0000ff]CStr[/COLOR][/COLOR](0), 0, 0)[/SIZE]
[SIZE=2]mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"play "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & musicAlias, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0)[/SIZE]

... i can play it only once (NOTE: this code will be played when i click the button)
  • 0

#8 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 12 April 2010 - 12:40 PM

When you want to play it again you should maybe stop it and then play it, or maybe you have to reopen it, sry I don't really remember but I know it's possible to play a sound more then once :)
  • 0

#9 AndrejCro

AndrejCro

    CC Newcomer

  • Just Joined
  • PipPip
  • 23 posts

Posted 12 April 2010 - 01:44 PM

[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If i press the button THEN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]musicPath = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"C:\kick.wav"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Open "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & Chr(34) & musicPath & Chr(34) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" alias "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"kick"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0) 
mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"play "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"kick"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0, 0)[/SIZE]
[SIZE=2]End If[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2]' timer goes every 300ms[/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] timer1_Tick([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] timer1.Tick
mciSendString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"close "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"kick"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](0), 0, 0)[/SIZE]
[SIZE=2]End Sub
[/SIZE][/SIZE]

look at this code, i have button and a timer that automatically closes the sound every 300ms and when it does,
i have my sound ready to be triggered again! (if i dont close it i cant play it)

im getting really frustrated, please when you have time test the code ... its of really big importance for me

thank you for your help,
cheers,
Andrej

p.s. its a funny stuff, this winmm.dll heh ^^'
  • 0

#10 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 13 April 2010 - 07:33 AM

I checked some old code where I used it:

mciSendString("close audio", CStr(0), 0, 0)
            mciSendString("Open " & Chr(34) & FilePath & Chr(34) & " alias audio", CStr(0), 0, 0)
            mciSendString("play audio", CStr(0), 0, 0)

If you have more then one sound you just need to use different aliases.
  • 0

#11 AndrejCro

AndrejCro

    CC Newcomer

  • Just Joined
  • PipPip
  • 23 posts

Posted 13 April 2010 - 07:49 AM

I dont believe this... haha

My code "refreshed" (closed) audio right after it was played and it couldnt play anymore..
his code closed audio right before he played it...

that was the BIG error =)
thank you for your help man,
see you!
Andrej
  • 0

#12 SUDHEESHR258

SUDHEESHR258

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 18 July 2010 - 01:57 AM

It works but how can i change the volume inthis method.whether i can change the bass or other properties of the sound.
Thanks in advance.if u can reply me in mail.my mail id is sudheeshr258@gmail.com
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download