playing sounds...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: playing sounds...

  1. #1
    Join Date
    Aug 2009
    Posts
    5

    playing sounds...

    Hi everyone. I'm working on a little project at the moment, and there's something I want the program to do, but I don't know the code to do it.

    Basically I just want the program to play a specific sound. I remember doing something like that at school, but it's been a while and I'm using a different version of VB.

    How do I play a sound?

  2. #2
    Join Date
    Aug 2009
    Posts
    12

    Re: playing sounds...

    What kind of sound do you want to play?

    For wave *.wav, use System.Media.SoundPlayer

    Code:
    Dim player As New System.Media.SoundPlayer("C:\song.wav")
    player.Play()
    For mp3, midi, wma, etc.. you may use the mciSendString API

    Code:
        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
    Use the following code to open and play
    mciSendString("Open ""C:\song.mp3"" Alias MySong", "", 0, 0)
    mciSendString("Play MySong", "", 0, 0)

    Use the following code the close
    mciSendString("Close MySong", "", 0, 0)

    The 'MySong' is user-defined alias, you can change to different name so that you can control multiple songs, or playing them at the same time!

  3. #3
    Join Date
    Aug 2009
    Posts
    5

    Re: playing sounds...

    I just wanted to play a very short sound. The project I'm doing is one that simulates the rate of fire of a gun, where you select the rate and it demonstrates how fast it is...

    I've figured out the code and mathematical formlas I needed to get the timer correct, but I wanted to add sound to it. I'm also having problems with trying to animate the picture of the gun I made :S

  4. #4
    Join Date
    Aug 2009
    Posts
    5

    Re: playing sounds...

    ---------------------------------------------------------------------------------
    Dim player As New System.Media.SoundPlayer("C:\song.wav")
    player.Play()
    ---------------------------------------------------------------------------------

    Using the code you gave me before...all went ok, except I need the file to play from the folder the program runs from. I don't know how to write the file location so that it does that...

    Also, I need the sound to overlap itself. When the program plays the sound, it cuts the sound off the last part off the previous sound to play the next time around. Since the user can change the rate at which the sound plays, if the rate-of-fire is faster than the sound file can play, the cut-off thing happens. Is there a way to let the previous sound play as the next one does so they overlap?
    Last edited by Gun Monkey; August 29th, 2009 at 12:30 AM. Reason: adding more info

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: playing sounds...

    There are more parameters to the (), after the filename. Press SPACE before the ) and you'll see Intellisense Options
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Aug 2009
    Posts
    12

    Re: playing sounds...

    Quote Originally Posted by Gun Monkey View Post
    ---------------------------------------------------------------------------------
    Dim player As New System.Media.SoundPlayer("C:\song.wav")
    player.Play()
    ---------------------------------------------------------------------------------

    Using the code you gave me before...all went ok, except I need the file to play from the folder the program runs from. I don't know how to write the file location so that it does that...

    Also, I need the sound to overlap itself. When the program plays the sound, it cuts the sound off the last part off the previous sound to play the next time around. Since the user can change the rate at which the sound plays, if the rate-of-fire is faster than the sound file can play, the cut-off thing happens. Is there a way to let the previous sound play as the next one does so they overlap?

    If you need to the file to be played from the folder that the program runs form, directly put the FILENAME then, e.g.

    Dim player As New System.Media.SoundPlayer("song.wav")

    In this case, the sound player will play the file from the application's start up folder.
    (This technique is appliable to other functions that open file as well)

    If you need the sound to be overlapped, umm. It seeems that soundplayer cannot play multiple of them at the same time even if you "new" a sound player.

    I think you have to use the second method that I suggest, the API approach.
    I have made an example for you. Remember to copy the above API definition to your form class.

    Code:
            mciSendString("Open ""song.wav"" Alias MySong", "", 0, 0)
            mciSendString("Play MySong", "", 0, 0)
    
            System.Threading.Thread.Sleep(1000)
    
            mciSendString("Open ""song.wav"" Alias MySong2", "", 0, 0)
            mciSendString("Play MySong2", "", 0, 0)
    Using this example, you should listen two audio broadcasting in an overlapped way (a one second delay)

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center