0

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

  • Use an audio library that gives you more control such as BASS.NET for instance http://bass.radio42.com/ – aybe Jul 07 '20 at 01:09

2 Answers2

0

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 ;)

Anes Hamdani
  • 50
  • 1
  • 7
  • 1
    Thank you for the reply, Anes! I don't mind this method, however I'm trying to stay away from 3rd party APIs as I don't want to add more resources and the additional distribution restrictions. Is there a way to solve this using DirectX or the internal sound player? Thanks again! – SilverSlash Jul 16 '20 at 10:29
  • Yes you can certainly use Either DirectX or `My.Computer.Audio.Play("Location", "ChooseAudioPlayMode")` but that won't give you much control as you cannot use basic controls such as Play/Pause/Position manipulating , I don't have much knowledge about DirectX but since you're not looking for control you can use `My.Computer.Audio.Play` it's smooth and can overlap the previous playing sound, try it and tell me what you got ^^ , PS: use MouseEnter event on label or image – Anes Hamdani Jul 16 '20 at 12:56
  • 1
    So, I just tried `My.Computer.Audio.Play` again and the sounds will not overlap. I have a theory, in order for it to overlap, should I use 2 of the same audio files to be called from different buttons? Will that make the audio files overlap and seem like 1 audio file playing twice and overlapping one another? – SilverSlash Jul 21 '20 at 01:40
  • Sorry for my previous comment i didn't understand what overlap means and google translate didn't give me much help , `My.Computer.Audio.Play` Can't overlap even if you have different audio sources since you are calling the same class in the same thread you are just resetting the internal media player of the class when calling it twice , you still can overlap using Bass Audio Library tho.Try these hopefully samples they may help [1](https://www.vbforums.com/showthread.php?498752-playing-one-*-wav-without-stopping-the-other)/[2](https://www.codeproject.com/Articles/9605/Play-Waves-in-VB-NET) – Anes Hamdani Jul 25 '20 at 12:59
0

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.

nips
  • 1
  • 2