+ Reply to Thread
Results 1 to 4 of 4

Thread: Playing sounds in VB.NET

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,777
    Blog Entries
    5

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Playing sounds in VB.NET

    Nicely done. +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    Re: Playing sounds in VB.NET

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

  4. #4
    Newbie Zerkei is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    8

    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 )

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Tutorial: Playing video files with Managed DirectX
    By ArekBulski in forum CSharp Tutorials
    Replies: 4
    Last Post: 01-22-2010, 02:59 AM
  2. Replies: 3
    Last Post: 11-02-2009, 07:40 PM
  3. Replies: 1
    Last Post: 11-02-2009, 07:48 AM
  4. Replies: 1
    Last Post: 11-02-2009, 07:43 AM
  5. Need a program created in VB.Net
    By Thomas in forum MarketPlace
    Replies: 0
    Last Post: 08-29-2007, 06:02 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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