www.pudn.com > MCI.zip > MCIControler.cpp, change:2010-12-02,size:9093b


////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h" 
#define _DLL_ 
#include <vfw.h> 
#include "MCIControler.h" 
#pragma comment(lib,"vfw32.lib") 
#ifndef _VFW_ 
#include <MMSystem.h> 
#include <Digitalv.h> 
#pragma comment(lib,"winmm.lib") 
#endif 
 
#ifndef _VFW_ 
MCIDEVICEID  m_wDeviceID; 
#endif 
HWND m_mciWnd; 
 
#ifndef _VFW_ 
////////////////////////////////////////////////////////////////////////// 
CMCIControler::CMCIControler() 
{ 
	m_dwLength = 0; 
	m_wDeviceID = -1; 
} 
#else 
////////////////////////////////////////////////////////////////////////// 
CMCIControler::CMCIControler(HWND hParent,HINSTANCE hInstance) 
{ 
	m_dwLength = 0; 
	m_mciWnd = NULL; 
	m_hParent  = hParent; 
	m_hInstance = hInstance; 
	if (m_mciWnd != NULL) 
		MCIWndDestroy(m_mciWnd); 
	m_mciWnd = MCIWndCreate(m_hParent,m_hInstance,0,NULL); 
//	ShowWindow(m_mciWnd,SW_HIDE); 
} 
#endif 
////////////////////////////////////////////////////////////////////////// 
CMCIControler::~CMCIControler() 
{ 
	MCIClose(); 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIOpen(LPCWSTR strPath) 
{ 
#ifndef _VFW_ 
	MCI_OPEN_PARMS mciOP; 
	mciOP.lpstrDeviceType =  NULL; 
	mciOP.lpstrElementName = strPath; 
 
	DWORD dwReturnOpen = mciSendCommand( 
		NULL, 
		MCI_OPEN, 
		MCI_OPEN_ELEMENT|MCI_WAIT|MCI_OPEN_SHAREABLE, 
		(DWORD)(LPVOID)&mciOP); 
	if (dwReturnOpen == 0) 
	{ 
		m_wDeviceID = mciOP.wDeviceID; 
		if (this->MCIGetLength(&m_dwLength) != 0) 
		{ 
			m_dwLength = 0; 
		} 
	} 
	else 
	{ 
		m_wDeviceID = -1; 
	} 
	return dwReturnOpen; 
#else 
	MCIWndOpen(m_mciWnd,strPath,0); 
	if (m_mciWnd == NULL) 
		return 0; 
	else 
		return 1; 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIClose(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	MCI_GENERIC_PARMS mciGP; 
	DWORD dwReturnClose = mciSendCommand( 
		m_wDeviceID, 
		MCI_CLOSE, 
		MCI_WAIT|MCI_NOTIFY, 
		(DWORD)(LPVOID)&mciGP); 
 
	return dwReturnClose; 
#else 
	MCIWndStop(m_mciWnd); 
	MCIWndDestroy(m_mciWnd); 
	return 0; 
#endif 
 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIPlay(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	MCI_PLAY_PARMS mciPP; 
	DWORD dwReturnPlay = mciSendCommand( 
		m_wDeviceID, 
		MCI_PLAY, 
		MCI_NOTIFY, 
		(DWORD)(LPVOID)&mciPP); 
	return dwReturnPlay; 
#else 
	DWORD dwReturn = MCIWndPlay(m_mciWnd); 
	ShowWindow(m_mciWnd,SW_HIDE); 
	return dwReturn; 
#endif 
 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIPause(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	MCI_GENERIC_PARMS mciGP; 
	DWORD dwReturnPause = mciSendCommand( 
		m_wDeviceID, 
		MCI_PAUSE, 
		MCI_NOTIFY|MCI_WAIT, 
		(DWORD)(LPVOID)&mciGP); 
	return dwReturnPause; 
#else 
	DWORD dwReturn = MCIWndPause(m_mciWnd); 
	return dwReturn; 
#endif 
 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIResume(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
	MCI_GENERIC_PARMS mciGP; 
	DWORD dwReturnResume = mciSendCommand( 
		m_wDeviceID, 
		MCI_RESUME, 
		MCI_NOTIFY|MCI_WAIT, 
		(DWORD)(LPVOID)&mciGP); 
	return dwReturnResume; 
#else 
	DWORD dwReturn = MCIWndResume(m_mciWnd); 
	return dwReturn; 
#endif 
 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIStop(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
	MCI_GENERIC_PARMS mciGP; 
	DWORD dwReturnStop = mciSendCommand( 
		m_wDeviceID, 
		MCI_STOP, 
		MCI_NOTIFY|MCI_WAIT, 
		(DWORD)(LPVOID)&mciGP); 
	return dwReturnStop; 
#else 
	DWORD dwReturn = MCIWndStop(m_mciWnd); 
	return dwReturn; 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCISetMute(BOOL bMute) 
{ 
#ifndef _VFW_ 
	MCI_SET_PARMS mciSP; 
	mciSP.dwAudio=MCI_SET_AUDIO_ALL ; 
	DWORD dwFlagsMute; 
	if(bMute) 
	{ 
		dwFlagsMute=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_OFF; 
	} 
	else 
	{ 
		dwFlagsMute=MCI_WAIT|MCI_SET_AUDIO|MCI_SET_ON; 
	} 
	dwFlagsMute=mciSendCommand(m_wDeviceID,MCI_SET,dwFlagsMute,(DWORD)(LPVOID)&mciSP); 
	return dwFlagsMute; 
#else 
	return 2000; 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
 
DWORD CMCIControler::MCIGetLength(DWORD* dwLength) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 0; 
 
	MCI_STATUS_PARMS mciSP; 
	mciSP.dwItem = MCI_STATUS_LENGTH; 
	DWORD dwReturnLength = mciSendCommand( 
		m_wDeviceID, 
		MCI_STATUS, 
		MCI_STATUS_ITEM, 
		(DWORD)(LPVOID)&mciSP); 
 
	if (dwReturnLength == 0) 
	{ 
		*dwLength = mciSP.dwReturn/1000; 
	} 
	return dwReturnLength; 
#else 
	DWORD dwReturn = MCIWndGetLength(m_mciWnd); 
	*dwLength = dwReturn; 
	return dwReturn; 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCIGetPos(DWORD* dwPos) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 0; 
 
	MCI_STATUS_PARMS mciSP; 
	mciSP.dwItem = MCI_STATUS_POSITION; 
	DWORD dwReturnPos = mciSendCommand( 
		m_wDeviceID, 
		MCI_STATUS, 
		MCI_STATUS_ITEM, 
		(DWORD)(LPVOID)&mciSP); 
 
	if (dwReturnPos == 0) 
	{ 
		*dwPos = mciSP.dwReturn/1000; 
	} 
	return dwReturnPos; 
#else 
	DWORD dwReturn = MCIWndGetPosition(m_mciWnd); 
	*dwPos = dwReturn; 
	return dwReturn; 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCISeekTo(DWORD dwTo) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	DWORD dwReturnSeekTo; 
	if (dwTo > 0 && dwTo < m_dwLength) 
	{ 
		MCI_SEEK_PARMS mciSP; 
		mciSP.dwTo = dwTo * 1000; 
		dwReturnSeekTo = mciSendCommand( 
			m_wDeviceID, 
			MCI_SEEK, 
			MCI_NOTIFY|MCI_TO, 
			(DWORD)(LPVOID)&mciSP); 
	} 
	else if (dwTo >= m_dwLength) 
	{ 
		dwReturnSeekTo = MCISeekToEnd(); 
		MCIStop(); 
	} 
	else 
	{ 
		dwReturnSeekTo = MCISeekToStart(); 
	} 
	return dwReturnSeekTo; 
#else 
	if (dwTo < 0) 
		return MCIWndSeek(m_mciWnd,0); 
	if (dwTo > m_dwLength) 
		return MCIWndSeek(m_mciWnd,m_dwLength); 
	return MCIWndSeek(m_mciWnd,dwTo); 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCISeekToStart(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	MCI_SEEK_PARMS mciSP; 
	DWORD dwReturnSeekToStart = mciSendCommand( 
		m_wDeviceID, 
		MCI_SEEK, 
		MCI_NOTIFY|MCI_SEEK_TO_START, 
		(DWORD)(LPVOID)&mciSP); 
	return dwReturnSeekToStart; 
#else 
	return MCIWndSeek(m_mciWnd,0); 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCISeekToEnd(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return 2000; 
 
	MCI_SEEK_PARMS mciSP; 
	DWORD dwReturnSeekToEnd = mciSendCommand( 
		m_wDeviceID, 
		MCI_SEEK, 
		MCI_NOTIFY|MCI_SEEK_TO_END, 
		(DWORD)(LPVOID)&mciSP); 
	return dwReturnSeekToEnd; 
#else 
	return MCIWndSeek(m_mciWnd,m_dwLength); 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
BOOL CMCIControler::IsPlaying(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return FALSE; 
	MCI_STATUS_PARMS mciSP; 
 
	mciSP.dwItem = MCI_STATUS_MODE; 
	DWORD dwReturnIsPlaying = mciSendCommand( 
		m_wDeviceID, 
		MCI_STATUS, 
		MCI_STATUS_ITEM, 
		(DWORD)(LPVOID)&mciSP); 
	if (dwReturnIsPlaying == 0) 
	{ 
		if (mciSP.dwReturn == MCI_MODE_PLAY) 
		{ 
			return TRUE; 
		} 
	} 
	return FALSE; 
#else 
	return MCIWndCanPlay(m_mciWnd); 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
BOOL CMCIControler::IsOpened(void) 
{ 
#ifndef _VFW_ 
	if (m_wDeviceID == -1) 
		return FALSE; 
	MCI_STATUS_PARMS mciSP; 
 
	mciSP.dwItem = MCI_STATUS_MODE; 
	DWORD dwReturnIsOpend = mciSendCommand( 
		m_wDeviceID, 
		MCI_STATUS, 
		MCI_STATUS_ITEM, 
		(DWORD)(LPVOID)&mciSP); 
	if (dwReturnIsOpend == 0) 
	{ 
		if (mciSP.dwReturn == MCI_MODE_PLAY  || 
			mciSP.dwReturn == MCI_MODE_OPEN  || 
			mciSP.dwReturn == MCI_MODE_PAUSE || 
			mciSP.dwReturn == MCI_MODE_STOP  || 
			mciSP.dwReturn == MCI_MODE_SEEK) 
		{ 
			return TRUE; 
		} 
	} 
	return FALSE; 
#else 
	return MCIWndCanWindow(m_mciWnd); 
#endif 
} 
////////////////////////////////////////////////////////////////////////// 
DWORD CMCIControler::MCISetVolume(DWORD dwVolume) 
{ 
#ifndef _VFW_ 
	MCI_DGV_SETAUDIO_PARMS mciSAP;   
	mciSAP.dwCallback = (DWORD)NULL;   
	mciSAP.dwItem = MCI_DGV_SETAUDIO_VOLUME;   
	mciSAP.dwValue = dwVolume; 
	DWORD dwReturnSetVolume = mciSendCommand( 
		m_wDeviceID, 
		MCI_SETAUDIO, 
		MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM, 
		(DWORD)(LPVOID)&mciSAP); 
	return dwReturnSetVolume; 
#else 
	if (dwVolume < 0) 
	{ 
		MCIWndSetVolume(m_mciWnd,0); 
		return 0; 
	} 
	if (dwVolume > 1000) 
	{ 
		MCIWndSetVolume(m_mciWnd,1000); 
		return 0; 
	} 
	MCIWndSetVolume(m_mciWnd,dwVolume); 
	return 0; 
#endif 
} 
//////////////////////////////////////////////////////////////////////////