-
August 27th, 2009, 12:21 AM
#1
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?
-
August 27th, 2009, 07:21 AM
#2
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!
-
August 28th, 2009, 10:36 PM
#3
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
-
August 29th, 2009, 12:21 AM
#4
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
-
August 29th, 2009, 02:47 PM
#5
Re: playing sounds...
There are more parameters to the (), after the filename. Press SPACE before the ) and you'll see Intellisense Options
-
August 29th, 2009, 07:07 PM
#6
Re: playing sounds...
Originally Posted by Gun Monkey
---------------------------------------------------------------------------------
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|