How to play a sound while another sound is already playing - VB.NET 2010

647 views Asked by At

Okay, so I've been ripping my hair out trying to solve this for the last week.

I need to play a sound when you hover over 2 separate images/labels. In the event that you shoot your cursor over both images/labels very quickly, the sound needs to play twice and be able to overlap one another, it can't wait for the first sound to stop before playing the second sound. Also both images/labels need to pull from the same .WAV audio file stored in my AppData, which means it needs to play the same .WAV file however many times is necessary and overlap as many times as necessary based on how fast you move your cursor over the images/labels.

I have tried all of the following.

DirectX.AudioVideoPlayback functions with the file path in a DIM. DirectX.DirectSound functions. (Can't remember all the ways I've tried). Media.SoundPlayer functions with the path stored in a "player.SoundLocation = "PATH" just before you call the act to play the file. My.Computer.Audio.Play("PATH").

I've tried mciSendString, I've tried to encase the play code in a thread (Threading.Thread(AddressOf NewSub)) multiple times... I've played with background sounds...

PLEASE! If anyone can help, I will find a way to repay you.

Thank you!!

2

There are 2 answers

4
Anes Hamdani On

By using BASS.Net (.net wrapper for Bass) you can play hundreds of sound at the same time by simply using this code : First you init bass

Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, System.IntPtr.Zero)

Then you declare an integer name it stream, this is the one you will be using to control the stream.

    Dim stream As Integer

Now you need to load the file to the stream

 stream = Bass.BASS_StreamCreateFile(FILELOCATION, 0, 0, BASSFlag.BASS_SAMPLE_FLOAT)

If you want to loop the stream you can use this

 If (Bass.BASS_ChannelFlags(stream, BASSFlag.BASS_DEFAULT, BASSFlag.BASS_DEFAULT) And BASSFlag.BASS_SAMPLE_LOOP) = BASSFlag.BASS_SAMPLE_LOOP Then
            ' loop flag was set, so remove it
            Bass.BASS_ChannelFlags(stream, BASSFlag.BASS_DEFAULT, BASSFlag.BASS_SAMPLE_LOOP)
        Else
            ' loop flag was not set, so set it
            Bass.BASS_ChannelFlags(stream, BASSFlag.BASS_SAMPLE_LOOP, BASSFlag.BASS_SAMPLE_LOOP)
        End If

To Control the stream use these: To play Bass.BASS_ChannelPlay(stream, False) Note the Second parameter (Boolean) is used to make the stream either play from begging or resumes To Pause Bass.BASS_ChannelPause(stream) To Stop Bass.BASS_ChannelStop(stream)

If you want further assistance just ask me I have a huge experience using Bass.net ^^

If my answer did help you don't forget to mark it as an answer so It helps the whole community ;)

0
nips On

You can use mciSendString to achieve multiplesound playing simutaneously, just use different sound name. To let a sound overlapping its self when later trigered. Yo may load the same sound with a different soundname, which may need some design. Currently, MCISendString support only one MIDI sequencer playing, together with multiple wav/mp3 sounds played simutaneously. A draw back of mciSendString is that it's filename has limited length (<?256 bytes or so). This will be a problem in a modern win10, which usually install the app in a deep directory with a long path. If this is the case, you can use specialdirectory, which are shallow in depth of directories.