www.pudn.com > dmtbfq.rar > MEDIA.CPP


// Sound.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 
////////////////////////////////////////////////////////////////////// 
 
CMedia::CMedia() 
{ 
	m_nDeviceID = 1; 
	dwResult = 0; 
	mciPlayParms.dwFrom = 0; 
	closed = stopped = true; 
	paused = false; 
} 
 
CMedia::~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 CMedia::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 CMedia::Play(CWnd* pWnd,LPCSTR pFileName) 
{ 
		//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 if(type != NONE) 
			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 CMedia::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); 
	} 
	stopped = true; 
	return dwResult; 
} 
 
DWORD CMedia::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 CMedia::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 CMedia::BeginRecord() 
{ 
	if(m_nDeviceID) 
	{ 
		dwResult = mciSendCommand (m_nDeviceID, MCI_RECORD,  
			NULL, (DWORD)(LPMCI_RECORD_PARMS)&mciRecordParms); 
		if(dwResult) 
			DisplayErrorMsg(dwResult); 
	} 
	return dwResult; 
} 
 
DWORD CMedia::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; 
} 
 
 
DWORD CMedia::OpenCDRom() 
{ 
	if(m_nDeviceID) 
	{ 
		if(!closed) 
			CloseDevice(); 
		dwResult = mciSendCommand (m_nDeviceID, MCI_SET,MCI_SET_DOOR_OPEN, NULL); 
		if(dwResult) 
			DisplayErrorMsg(dwResult); 
	} 
	return dwResult; 
} 
 
DWORD CMedia::CloseCDRom() 
{ 
	if(m_nDeviceID) 
	{ 
		dwResult = mciSendCommand (m_nDeviceID, MCI_SET,MCI_SET_DOOR_CLOSED, NULL); 
		if(dwResult) 
			DisplayErrorMsg(dwResult); 
	} 
	return dwResult; 
} 
 
DWORD CMedia::Seek(int nMiniute, int nSecond) 
{ 
	if(m_nDeviceID) 
	{ 
		mciSeekParms.dwTo = (nMiniute * 60 + nSecond) * 1000; 
		dwResult = mciSendCommand (m_nDeviceID, MCI_SEEK,MCI_TO|MCI_WAIT,  
			(DWORD) (LPMCI_SEEK_PARMS)&mciSeekParms); 
		if(dwResult) 
			DisplayErrorMsg(dwResult); 
	} 
	return dwResult; 
}