以下是我自己尝试写的调用direct sound实现播放音频文件代码,编译是通过了,但执行后出现错误~不知道是什么原因~请高手指点~万分感谢
void CAudioPlayerDlg::OnButtonPlay()
{
// TODO: Add your control notification handler code here
LPDIRECTSOUND lpds = NULL;
InitDSound(m_hWnd, lpds);
CAudioPlayerDlg::lpdsb[0] = LoadStatic(lpds,"Audio.wav");
PlayStatic(CAudioPlayerDlg::lpdsb[0]);
}
BOOL CAudioPlayerDlg::InitDSound(HWND hwnd, LPDIRECTSOUND lpds)
{
HRESULT hr;
// Create DirectSound.
if ( FAILED( hr = DirectSoundCreate(NULL, &lpds, NULL ) ) )
return FALSE;
// Set cooperative level.
if ( FAILED( hr = lpds->SetCooperativeLevel(hwnd, DSSCL_PRIORITY ) ) )
return FALSE;
return TRUE;
}
LPDIRECTSOUNDBUFFER CAudioPlayerDlg::LoadStatic (LPDIRECTSOUND lpds, LPSTR lpzFileName)
{
WAVEFORMATEX *pwfx;
HMMIO hmmio;
MMCKINFO mmckinfo;
MMCKINFO mmckinfoParent;
LPDIRECTSOUNDBUFFER lpdsbStatic = NULL;
LPVOID lpvAudio1;
DWORD dwBytes1;
DSBUFFERDESC dsbdesc;
UINT cbBytesRead;
if (WaveOpenFile(lpzFileName, &hmmio, &pwfx, &mmckinfoParent ) != 0 )
return FALSE;
if (WaveStartDataRead( &hmmio, &mmckinfo, &mmckinfoParent ) != 0 )
return FALSE;
if ( lpdsbStatic == NULL )
{
memset( &dsbdesc, 0, sizeof( DSBUFFERDESC ) );
dsbdesc.dwSize = sizeof( DSBUFFERDESC );
dsbdesc.dwFlags = DSBCAPS_STATIC;
dsbdesc.dwBufferBytes = mmckinfo.cksize;
dsbdesc.lpwfxFormat = pwfx;
if ( FAILED( lpds->CreateSoundBuffer(&dsbdesc, &lpdsbStatic, NULL ) ) )
{
WaveCloseReadFile(&hmmio, &pwfx);
return FALSE;
}
}
lpdsbStatic->Lock( 0, 0, &lpvAudio1, &dwBytes1, NULL, NULL, DSBLOCK_ENTIREBUFFER );
WaveReadFile( hmmio, dwBytes1, ( BYTE * )lpvAudio1,&mmckinfo, &cbBytesRead );
lpdsbStatic->Unlock( lpvAudio1, dwBytes1, NULL, 0 );
WaveCloseReadFile( &hmmio, &pwfx );
return lpdsbStatic;
}
void CAudioPlayerDlg::PlayStatic(LPDIRECTSOUNDBUFFER lpdsbStatic)
{
if ( lpdsbStatic == NULL ) return;
lpdsbStatic->SetCurrentPosition(0);
lpdsbStatic->Play(0, 0, 0);
}
已解决~