困扰达20多天的两个问题……
以下为我的程序的几个片段:
(1)
HRESULT CAudioPlay::OnSample( /* [in] */ DWORD dwOutputNum,
/* [in] */ QWORD cnsSampleTime,
/* [in] */ QWORD cnsSampleDuration,
/* [in] */ DWORD dwFlags,
/* [in] */ INSSBuffer __RPC_FAR *pSample,
/* [in] */ void __RPC_FAR *pvContext )
{
//
// Make sure its Audio sample
//
if( m_dwAudioOutputNum != dwOutputNum )
{
return( S_OK ) ;
}
HRESULT hr = S_OK ;
BYTE *pData = NULL ;
DWORD cbData = 0 ;
hr = pSample->GetBufferAndLength( &pData, &cbData ) ;
if( FAILED( hr ) )
{
return hr ;
}
LPWAVEHDR pwh = ( LPWAVEHDR ) new BYTE[ sizeof( WAVEHDR ) + cbData ];
if( NULL == pwh )
{
return( HRESULT_FROM_WIN32( GetLastError() ) ) ;
}
pwh->lpData = ( LPSTR )&pwh[1] ;
pwh->dwBufferLength = cbData;
pwh->dwBytesRecorded = cbData ;
pwh->dwUser = (DWORD)cnsSampleTime ;
pwh->dwLoops = 0 ;
pwh->dwFlags = 0 ;
CopyMemory( pwh->lpData, pData, cbData );
//
// Prepare the header for playing
//
hr = waveOutPrepareHeader( m_hWaveOut, pwh, sizeof(WAVEHDR) );
if( hr == MMSYSERR_NOERROR )
{
//
// Send the sample to the wave output device
//
hr = waveOutWrite( m_hWaveOut, pwh, sizeof( WAVEHDR ) ) ;
}
if( hr == MMSYSERR_NOERROR )
{
InterlockedIncrement( &m_cHeadersLeft ) ;
}
else
{
hr = Stop() ;
if( SUCCEEDED( hr ) )
{
// MessageBox( g_hwndDialog, _T("Wave function failed"), _T("WM Audio Player Sample"), MB_OK ) ;
}
}
return S_OK ;
}
(2)
DWORD WINAPI CAudioPlay::OnWaveOutThread(LPVOID lpParameter)
{
CAudioPlay* pThis = (CAudioPlay*) lpParameter ;
//
// Redirect the processing to a non static member
// function of the CAudioPlay class
//
pThis->OnWaveOutMsg() ;
return 0 ;
}
(3)
void CAudioPlay::OnWaveOutMsg()
{
MSG uMsg;
HRESULT hr = S_OK ;
//
// Create the message queue
//
PeekMessage( &uMsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//
// Message Que has been created.Lets get the messages
//
while( 0 != GetMessage( &uMsg, NULL, 0, 0 ) )
{
switch( uMsg.message )
{
case WOM_DONE:
{
//
// Unprepare the Waveheader, as it has already been played
//
LPWAVEHDR pwh = ( LPWAVEHDR )uMsg.wParam ;
hr = waveOutUnprepareHeader( m_hWaveOut, pwh, sizeofWAVEHDR) ) ; if( !m_bNetReading )
{
m_nDurationLeft = m_nTotalDuration - (QWORD)(pwh->dwUser) ;
// SetTime((UINT)m_nDurationLeft/100000,(UINT) m_nTotalDuration/100000 ) ;
}
if( hr == MMSYSERR_NOERROR )
{
InterlockedDecrement( &m_cHeadersLeft );
}
else
{
if(m_cHeadersLeft == 0)
hr = Stop() ;
if( SUCCEEDED( hr ) )
{
AfxMessageBox( _T("Wave function failed")) ;
}
}
if( m_bEOF && ( 0 == m_cHeadersLeft ) )
{
hr = waveOutClose(m_hWaveOut) ;
if( hr == MMSYSERR_NOERROR )
{
m_hWaveOut = NULL ;
}
m_nDurationLeft = m_nTotalDuration ;
if( m_bNetReading )
{
Close() ;
SetCurrentStatus( CLOSED ) ;
// SetCurrentStatus( READY ) ;
}
else
;
// SetCurrentStatus( STOP ) ;
}
}
break ;
case WOM_CLOSE:
PostQuitMessage( 0 );
break ;
}
}
}
大虾你务必耐着性子把他看看,为什么我在(1)中用pwh申请的内存在(3)中释放不掉??我试着用:
if(pwh)
{
delete[] (BYTE*)pwh;
pwh = NULL;
}
还是不行,有些时候还会内存泄露!