📄 sound.cpp
字号:
// Media.cpp: implementation of the CSound class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Multimedia.h"
#include "Media.h"
#include "string.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Media::CMedia()
{
m_nDeviceID = 1;
m_nElementID = 0;
dwResult = 0;
mciPlayParms.dwFrom = 0;
closed = stopped = true;
paused = false;
}
CSound::~CMedia()
{
}
DWORD CMedia::OpenDevice(LPCSTR pFileName,LPCSTR pFileExt)
{
if(!closed)
{
CloseDevice();
closed = true;
m_nDeviceID = 1;
}
if(paused)
paused = false;
if (m_nDeviceID)
{
if(strcmp(pFileExt,"WAV") == 0 || strcmp(pFileExt,"wav") == 0)
{
mciOpenParms.lpstrDeviceType = "waveaudio";
type = WAVE;
}
else if(strcmp(pFileExt,"MID") == 0 || strcmp(pFileExt,"mid") == 0)
{
mciOpenParms.lpstrDeviceType="sequencer";
type = MIDI;
}
else if(strcmp(pFileExt,"CD") == 0 || strcmp(pFileExt,"cd") == 0)
{
mciOpenParms.lpstrDeviceType = "cdaudio";
type = CD;
}
else if(strcmp(pFileExt,"AVI") == 0 || strcmp(pFileExt,"avi") == 0)
{
mciOpenParms.lpstrDeviceType = "avivideo";
type = AVI;
}
//open the sound device
mciOpenParms.wDeviceID = 0;
mciOpenParms.lpstrElementName = pFileName;
dwResult = mciSendCommand(NULL, MCI_OPEN, MCI_WAIT|MCI_OPEN_TYPE|
MCI_OPEN_ELEMENT,(DWORD)(LPMCI_OPEN_PARMS)&mciOpenParms);
//save device identifier,will use eith other MCI commands
m_nDeviceID = mciOpenParms.wDeviceID;
//display error message if failed
if(dwResult)
DisplayErrorMsg(dwResult);
else
{
stopped = closed =false;
}
}
//return result of MCI operation
return dwResult;
}
void CSound::DisplayErrorMsg(DWORD dwError)
{
//check if there was an error
if(dwError)
{
//character string that contains error message
char szErrorMsg[MAXERRORLENGTH];
//retrieve string associated error message
if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg)))
strcpy(szErrorMsg,"Unknown Error");
//display error string in message box
AfxMessageBox(szErrorMsg);
}
}
DWORD CSound::Play(CWnd* pWnd,LPCSTR pFileName)
{
m_nElementID=mciOpenParms.wDeviceID;
//instruct device to play file
if(paused && type == WAVE)
{
dwResult = mciSendCommand(m_nDeviceID,MCI_RESUME,MCI_WAIT,
(DWORD) (LPMCI_GENERIC_PARMS) &mciGenericParms);
paused = false;
}
else
dwResult = mciSendCommand(m_nDeviceID,MCI_PLAY,
MCI_FROM,(DWORD)(LPMCI_PLAY_PARMS)&mciPlayParms);
//display error and close element if failed
if(dwResult)
{
DisplayErrorMsg(dwResult);
Stop();
stopped = true;
}
else
stopped = false;
//return result of MCI operation
return dwResult;
}
DWORD CSound::Stop()
{
//close if element is currently open
if(m_nDeviceID)
{
dwResult = mciSendCommand(m_nDeviceID,MCI_STOP,
MCI_WAIT,(DWORD) (LPMCI_GENERIC_PARMS) &mciStopParms);
//display error message if failed
if(dwResult)
DisplayErrorMsg(dwResult);
//set identifier to closed state
else
m_nDeviceID=0;
}
stopped = true;
return dwResult;
}
DWORD CSound::CloseDevice()
{
//close if currently open
if(m_nDeviceID && !stopped)
{
//close the MCI device
dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,MCI_WAIT,NULL);
//display error message if failed
if(dwResult)
DisplayErrorMsg(dwResult);
//set identifier to close state
else
m_nDeviceID=0;
}
//return result of MCI operation
closed = true;
return dwResult;
}
DWORD CSound::Pause()
{
if(m_nDeviceID)
{
dwResult = mciSendCommand (m_nDeviceID,
MCI_PAUSE, MCI_WAIT,(DWORD)(LPMCI_GENERIC_PARMS) &mciGenericParms);
if(dwResult)
{
DisplayErrorMsg(dwResult);
return dwResult;
}
}
paused = true;
return dwResult;
}
DWORD CSound::BeginRecord()
{
if(m_nDeviceID)
{
dwResult = mciSendCommand (m_nDeviceID, MCI_RECORD,
NULL, (DWORD)(LPMCI_RECORD_PARMS)&mciRecordParms);
if(dwResult)
DisplayErrorMsg(dwResult);
}
return dwResult;
}
DWORD CSound::SaveRecord(LPCSTR pFilename)
{
if(m_nDeviceID)
{
mciSaveParms.lpfilename = pFilename;
dwResult = mciSendCommand (m_nDeviceID, MCI_SAVE,
MCI_SAVE_FILE | MCI_WAIT,(DWORD)(LPMCI_SAVE_PARMS) &mciSaveParms);
if(dwResult)
DisplayErrorMsg(dwResult);
}
return dwResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -