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