📄 playsound1.cpp
字号:
// PlaySound1.cpp: implementation of the CPlaySound1 class.
//
//////////////////////////////////////////////////////////////////////
#include<afxwin.h>
#include<mmsystem.h>
#include<mmreg.h>
#include "PlaySound1.h"
#include "Display.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(PlaySound1, CWinThread)
BEGIN_MESSAGE_MAP(PlaySound1, CWinThread)
ON_THREAD_MESSAGE(WM_PLAYSOUND_STARTPLAYING, OnStartPlaying)
ON_THREAD_MESSAGE(WM_PLAYSOUND_STOPPLAYING, OnStopPlaying)
ON_THREAD_MESSAGE(WM_PLAYSOUND_PLAYBLOCK, OnWriteSoundData)
ON_THREAD_MESSAGE(MM_WOM_DONE, OnEndPlaySound1Data)
ON_THREAD_MESSAGE(WM_PLAYSOUND_ENDTHREAD,OnEndThread)
END_MESSAGE_MAP()
PlaySound1::PlaySound1()
{
}
PlaySound1::PlaySound1(CDialog *dialog)
{
log.Open("playfile.txt",CFile::modeCreate | CFile::modeWrite);
dlg=dialog;
/* // For GSM COMPRESSION - change PLAYBUFFER 600 in PlaySound1.h
// remove the comment for structure m_waveFormatEx in PlaySound1.h
memset(&m_WaveFormatEx,0x00,sizeof(m_WaveFormatEx));
m_WaveFormatEx.wfx.wFormatTag = WAVE_FORMAT_GSM610;
m_WaveFormatEx.wfx.nChannels = 1;
m_WaveFormatEx.wfx.wBitsPerSample = 0;
m_WaveFormatEx.wfx.cbSize = 2;
m_WaveFormatEx.wfx.nSamplesPerSec = SAMPLEPSEC;
m_WaveFormatEx.wfx.nAvgBytesPerSec = 1625; //(SAMPLEPSEC/320)*65 ;
m_WaveFormatEx.wfx.nBlockAlign = 65;
m_WaveFormatEx.wSamplesPerBlock=320;
*/
memset(&m_WaveFormatEx,0x00,sizeof(m_WaveFormatEx));
m_WaveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
m_WaveFormatEx.nChannels = 1;
m_WaveFormatEx.wBitsPerSample = 8;
m_WaveFormatEx.cbSize = 0;
m_WaveFormatEx.nSamplesPerSec = SAMPLEPSEC;
m_WaveFormatEx.nAvgBytesPerSec = SAMPLEPSEC ;
m_WaveFormatEx.nBlockAlign = 1;
Playing = FALSE;
log.WriteString("\n In the constructor of Play sound");
}
PlaySound1::~PlaySound1()
{
log.Close();
}
BOOL PlaySound1::InitInstance()
{
return TRUE;
}
int PlaySound1::ExitInstance()
{
return CWinThread::ExitInstance();
}
///////////////////////////////////////////
// Start the playing thread...called from Display class
// for using PostThreadMessage()
//
LRESULT PlaySound1::OnStartPlaying(WPARAM wParam, LPARAM lParam)
{
MMRESULT mmReturn = 0;
if(Playing==TRUE)
return FALSE;
log.WriteString("\n Starting playing");
// open wavein device
mmReturn = ::waveOutOpen( &m_hPlay, WAVE_MAPPER,
&m_WaveFormatEx, ::GetCurrentThreadId(), 0, CALLBACK_THREAD);
/*//For GSM compression use this code
mmReturn = ::waveOutOpen( &m_hPlay, WAVE_MAPPER,
&m_WaveFormatEx.wfx, ::GetCurrentThreadId(), 0, CALLBACK_THREAD);
*/
if(mmReturn )
displayError(mmReturn,"PlayStart");
else
{
Playing = TRUE;
DWORD volume=0xffffffff;
char str[100];
if(!waveOutSetVolume(m_hPlay,volume))
{
volume=0;
if(!waveOutGetVolume(m_hPlay,&volume))
{
wsprintf(str,"\n Volume is %lx",volume);
log.WriteString(str);
}
}
}
return TRUE;
}
void PlaySound1::displayError(int code,char mesg[])
{
char errorbuffer[MAX_PATH];
char errorbuffer1[MAX_PATH];
waveOutGetErrorText( code,errorbuffer,MAX_PATH);
sprintf(errorbuffer1,"PLAY : %s :%x:%s",mesg,code,errorbuffer);
AfxMessageBox(errorbuffer1);
}
LRESULT PlaySound1::OnStopPlaying(WPARAM wParam, LPARAM lParam)
{
MMRESULT mmReturn = 0;
if(Playing==FALSE)
return FALSE;
log.WriteString("\n Stop playing");
mmReturn = ::waveOutReset(m_hPlay);
if(!mmReturn)
{
Playing = FALSE;
Sleep(500);
mmReturn = ::waveOutClose(m_hPlay);
}
return mmReturn;
}
LRESULT PlaySound1::OnEndPlaySound1Data(WPARAM wParam, LPARAM lParam)
{
LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;
if(lpHdr)
{
::waveOutUnprepareHeader(m_hPlay, lpHdr, sizeof(WAVEHDR));
}
return ERROR_SUCCESS;
}
////////////////////////////////////////////////////
// Display calls this function using PostThreadMesssage
// whenever voice data is received.....
//
LRESULT PlaySound1::OnWriteSoundData(WPARAM wParam, LPARAM lParam)
{
MMRESULT mmResult = 0;
LPWAVEHDR lpHdr=(LPWAVEHDR)lParam;
log.WriteString("\n Playing data ");
if(lpHdr==NULL)
return ERROR_SUCCESS;
if(Playing)
{
mmResult = ::waveOutPrepareHeader(m_hPlay, lpHdr, sizeof(WAVEHDR));
if(mmResult)
{
log.WriteString("\nError while preparing header");
return ERROR_SUCCESS;
}
mmResult = ::waveOutWrite(m_hPlay, lpHdr, sizeof(WAVEHDR));
if(mmResult)
{
log.WriteString("\nError while writing to device");
return ERROR_SUCCESS;
}
}
return ERROR_SUCCESS;
}
///////////////////////////////
// Quit the thread
//
//
LRESULT PlaySound1::OnEndThread(WPARAM wParam, LPARAM lParam)
{
if(Playing==TRUE)
OnStopPlaying(0,0);
log.WriteString("\nEnding the play device");
::PostQuitMessage(0);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -