+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Playing sounds in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Playing sounds in VB.NET

    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:


    [highlight=VB.NET]My.Computer.Audio.Play(musicPath)[/highlight]

    To stop it you just use:

    [highlight=VB.NET]My.Computer.Audio.Stop[/highlight]

    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.


    [highlight=VB.NET]My.Computer.Audio.Play(musicPath, AudioPlayMode.Background)
    My.Computer.Audio.Play(musicPath, AudioPlayMode.BackgroundLoop)
    My.Computer.Audio.Play(musicPath, AudioPlayMode.WaitToComplete)[/highlight]








    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:


    [highlight=VB.NET]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[/highlight]


    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:


    [highlight=VB.NET] Dim musicAlias As String = "myAudio"
    Dim musicPath As String = "C:\Users\Public\Music\Sample Music\Symphony_No_3.wma" [/highlight]

    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:



    [highlight=VB.NET] mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)[/highlight]


    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:


    [highlight=VB.NET]mciSendString("play " & musicAlias, CStr(0), 0, 0)[/highlight]

    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:



    [highlight=VB.NET] '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)[/highlight]



    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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Playing sounds in VB.NET

    Nicely done. +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Playing sounds in VB.NET

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

  5. #4
    Zerkei is offline Newbie
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Re: Playing sounds in VB.NET

    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 )

  6. #5
    AndrejCro is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Playing sounds in VB.NET

    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?

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

    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...

  7. #6
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Playing sounds in VB.NET

    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

  8. #7
    AndrejCro is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Playing sounds in VB.NET

    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:

    Code:
    musicPath = "C:\Documents and Settings\Administrator\Desktop\MusicMe\MusicMe\bin\Debug\Drums\kick.wav"
    musicAlias = "kick"
    mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)
    mciSendString("play " & musicAlias, CStr(0), 0, 0)
    ... i can play it only once (NOTE: this code will be played when i click the button)

  9. #8
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Playing sounds in VB.NET

    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

  10. #9
    AndrejCro is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Playing sounds in VB.NET

    Code:
    
    If i press the button THEN
    musicPath = "C:\kick.wav"
    mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & "kick", CStr(0), 0, 0) 
    mciSendString("play " & "kick", Nothing, 0, 0)
    End If
     
    ' timer goes every 300ms
    PrivateSub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
    mciSendString("close " & "kick", CStr(0), 0, 0)
    End Sub
    
    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 '

  11. #10
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Playing sounds in VB.NET

    I checked some old code where I used it:

    Code:
                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.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 5 users browsing this thread. (0 members and 5 guests)

Similar Threads

  1. How to make Jar file using sounds
    By Serialcek in forum Java Help
    Replies: 18
    Last Post: 03-09-2011, 04:07 PM
  2. sounds synthesis programming
    By aelgoa in forum General Programming
    Replies: 1
    Last Post: 06-01-2010, 08:12 PM
  3. Need help changing sounds in Betfair Poker client...
    By football in forum General Programming
    Replies: 7
    Last Post: 05-01-2009, 06:51 AM
  4. Sounds and Sound Loop Resources
    By TVDinner in forum Website Design
    Replies: 0
    Last Post: 03-10-2007, 06:20 AM
  5. Are you playing very often
    By xtraze in forum Video Game Talk
    Replies: 10
    Last Post: 02-25-2007, 11:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts