how do I play a sound from a prefetched buffer?

R

Richard

Hi,

I want to be able to play a sound wave in VB / .NET. I don't mind how it is
accomplished except that I want to supply the buffer prefilled with the wave
file. Due to the way my code operates, no .wav files will be present.
Instead, I read the appropriate sound buffer from the data file but I then
want to pass this to whatever function so that the it can be played.

I thought about writing the buffer out to a temp .wav file but I'd rather
not if there is another way of doing this.

I am been playing about with the winmm.dll but I can't see if it's possible
to actually play a sound buffer, it only appears to work with a wave file.

Can anybody help me?

Regards and thanks,

Richard
 
H

Herfried K. Wagner [MVP]

* "Richard said:
I want to be able to play a sound wave in VB / .NET. I don't mind how it is
accomplished except that I want to supply the buffer prefilled with the wave
file. Due to the way my code operates, no .wav files will be present.
Instead, I read the appropriate sound buffer from the data file but I then
want to pass this to whatever function so that the it can be played.

I thought about writing the buffer out to a temp .wav file but I'd rather
not if there is another way of doing this.

I am been playing about with the winmm.dll but I can't see if it's possible
to actually play a sound buffer, it only appears to work with a wave file.

If the whole wave file is loaded into the memory, have a look here:

<http://www.google.de/[email protected]>
 
R

Richard

Thanks Herfried, it got me a little bit closer. My code is:

Dim data As IntPtr

data =
System.Runtime.InteropServices.Marshal.AllocHGlobal(CInt(mBuffer.Length))
System.Runtime.InteropServices.Marshal.Copy(mBuffer, 0, data,
CInt(mBuffer.Length))
PlaySound(data, IntPtr.Zero, CType(PlaySoundFlags.SND_MEMORY Or
PlaySoundFlags.SND_SYNC, Integer))
System.Runtime.InteropServices.Marshal.FreeHGlobal(data)

I prefetch variable mBuffer from the .wav file itself (actually from a
massive file containing all the waves) but when I play it all I get is
whitenoise. If I write mBuffer out to a .wav file and play it from there
(which I don't want to do though) it plays fine, so I know mBuffer contains
the wave, but I can't get the system to do it.

I suspected the problem might be in the Marshal.Copy line when I copy the
contents of mBuffer to memory, but the Copy appears to perform okay without
raising any exceptions.

Any ideas?

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top