使用DirectSound播放WAV的方法

使用DirectSound播放WAV的方法

Imports DxVBLib

Public Class DirectSound
Dim dx As New DxVBLib.DirectX7
Dim ds As DxVBLib.DirectSound

Dim Buf As DxVBLib.DirectSoundBuffer '创建缓冲
Dim waveformatex As Object
Dim sdbuffer As DxVBLib.DSBUFFERDESC
Dim wvformat As DxVBLib.WAVEFORMATEX

Dim mHwnd As IntPtr

Public Sub New(ByVal hwnd As IntPtr)
mHwnd = hwnd
ds = dx.DirectSoundCreate("")
End Sub

Public Sub Play()
Call ds.SetCooperativeLevel(mHwnd, DxVBLib.CONST_DSSCLFLAGS.DSSCL_PRIORITY)

sdbuffer.lFlags = CONST_DSBCAPSFLAGS.DSBCAPS_STICKYFOCUS

Dim wave As String = "waveformatex"

wvformat.nSize = wave.Length
wvformat.nChannels = 1
wvformat.lSamplesPerSec = 1000 ' 44100
wvformat.nBitsPerSample = 16
wvformat.nBlockAlign = wvformat.nBitsPerSample / 8 * wvformat.nChannels
wvformat.lAvgBytesPerSec = wvformat.lSamplesPerSec * wvformat.nBlockAlign * 10

'音声をバッファに貯める
Buf = ds.CreateSoundBufferFromFile("c:/风之彩 钢琴曲.wav", sdbuffer, wvformat)
'ここに可変のwaitを入れたいのです。

'再生
Call Buf.Play(CONST_DSBPLAYFLAGS.DSBPLAY_LOOPING)
'再生終了するまで次の処理には行かない。
Do While Buf.GetStatus = CONST_DSBSTATUSFLAGS.DSBSTATUS_PLAYING
Application.DoEvents()
Loop
End Sub
End Class