www.pudn.com > MPlayer0623.zip > Media.cpp


// Media.cpp: implementation of the CMedia class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "MPlayer.h" 
#include "Media.h" 
//#include "IVolume.h" 
//#include "volumeoutwave.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
#if _DEBUG 
void DispatchMciError(MCIERROR mciError) 
{ 
	if(mciError)		//get error 
	{ 
		char chError[100]; 
		mciGetErrorString(mciError,chError,lstrlen(chError)); 
		AfxMessageBox(chError); 
	} 
} 
#endif 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CMedia::CMedia() 
{ 
	m_pause=FALSE; 
} 
 
CMedia::~CMedia() 
{ 
 
} 
 
void CMedia::Create(LPCTSTR strMedia,CString& szError) 
{ 
	m_strMedia=strMedia; 
	MCIERROR mciError; 
	CString strCommand; 
	int iIndex=m_strMedia.ReverseFind('.'); 
	m_mediaSuffix=m_strMedia.Mid(iIndex+1,3);//get the file's suffix 
	m_mediaSuffix.MakeLower(); 
	//////////mcisendString can not open media file whose filename has BLANK character  
 
	///////////select driver//////////////////////////////////////////// 
/*	if(m_mediaSuffix=="avi") //avi file 
		strCommand.Format("open avivideo!%s alias myMedia",strMedia); 
	else if(m_mediaSuffix=="wav") //wave form file 
		strCommand.Format("open waveaudio!%s alias myMedia",strMedia); 
	else if(m_mediaSuffix=="cda") 
		strCommand.Format("open cdaudio!%s alias myMedia",strMedia); 
	else 
		strCommand.Format("open MPEGvideo2!%s alias myMedia",strMedia); 
///////////////////////////////////////////////////////////////////// 
	mciError=mciSendString(strCommand,NULL,0,NULL); 
 
*/ 
///////////////////////////////////////////////////////////// 
////test mciSendCommand//////////////////////// 
	MCI_OPEN_PARMS mciOpen; 
	if(m_mediaSuffix=="avi") 
		mciOpen.lpstrDeviceType="avivideo"; 
	else if(m_mediaSuffix=="wav") //wave form file 
		mciOpen.lpstrDeviceType="waveaudio"; 
	else if(m_mediaSuffix=="cda") 
		mciOpen.lpstrDeviceType="cdaudio"; 
	else 
		mciOpen.lpstrDeviceType="MPEGvideo2"; 
		 
	mciOpen.lpstrElementName=m_strMedia; 
	mciOpen.lpstrAlias="myMedia"; 
	mciError=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_OPEN_ALIAS|MCI_OPEN_TYPE ,(DWORD)&mciOpen); 
////////////////////////////////////////////// 
	if(mciError)		//get error 
	{ 
		char chError[100]; 
		mciGetErrorString(mciError,chError,sizeof(chError)); 
		szError.Format("%s",chError); 
	} 
	else		//no error 
		szError="OK"; 
 
	if(m_mediaSuffix=="mp3" || m_mediaSuffix=="wav" || m_mediaSuffix=="cda") 
		m_bIsVideo=FALSE; 
	else 
		m_bIsVideo=TRUE; 
} 
 
void CMedia::Play(int iStartPos) 
{	//the default parameter is CURRENTPOS 
	ASSERT(iStartPos>=-1); 
	m_pause=FALSE; 
	if(iStartPos==CURRENTPOS)	 
		mciSendString("play myMedia",NULL,0,NULL); 
	else 
	{ 
		CString strCommand; 
		strCommand.Format("play myMedia from %d",iStartPos); 
		mciSendString(strCommand,NULL,0,NULL); 
	} 
} 
 
void CMedia::SetSpeed(int iSpeed) 
{ 
 
} 
 
void CMedia::Close() 
{ 
	mciSendString("close myMedia",NULL,0,NULL); 
} 
 
void CMedia::Stop() 
{ 
	mciSendString("stop myMedia",NULL,0,NULL); 
	mciSendString("seek myMedia to start",NULL,0,NULL); 
} 
 
void CMedia::Pause() 
{ 
	if(m_pause)	//in pause state, then resume play 
		mciSendString("resume myMedia",NULL,0,NULL);	 
	else//else pause  
		mciSendString("pause myMedia", NULL,0,NULL); 
	m_pause=!m_pause; 
} 
 
LONG CMedia::GetLength() 
{ 
	MCIERROR mciError;	 
	char chLength[50]; 
	if(!m_bIsVideo)	//music file ,get total length in milliseconds 
	{ 
		mciError=mciSendString("set myMedia time format milliseconds",NULL,0,NULL); 
#if _DEBUG 
		DispatchMciError(mciError); 
#endif 
	}else //video file ,get total frames 
	{ 
		mciError=mciSendString("set myMedia time format frames",NULL,0,NULL); 
#if _DEBUG 
		DispatchMciError(mciError); 
#endif 
	} 
	 
	mciError=mciSendString("status myMedia length",chLength,sizeof(chLength),NULL); 
#if _DEBUG 
		DispatchMciError(mciError); 
#endif 
	if(mciError) 
		return 0;	//0 means can not get length: ASF file can not get length 
	CString strLength; 
	strLength.Format("%s",chLength); 
	return (LONG)(StrToLong((LPCTSTR)strLength)); 
	// music return in milliseconds  
	// video return in frames 
} 
//GetHwnd(), 
//Get the handle of the VIDEO play window. 
//this function only for Video media who has a play window 
HWND CMedia::GetHwnd() 
{ 
	if(!IsVideo())	//music media 
		return NULL; 
	 
	char chHandle[100]; 
	MCIERROR mciError=mciSendString("status myMedia window handle",chHandle,sizeof(chHandle),NULL);	 
#if _DEBUG 
	DispatchMciError(mciError); 
#endif 
	CString strHandle; 
	strHandle.Format("%s",chHandle); 
	return (HWND)(StrToLong((LPCTSTR)strHandle)); 
} 
//the setVolume function have some problem in my computer 
//I can not get the device handle while the media is playing 
//that is, I can not get the audio divice handle while the handle is open by my media  
//I had used many ways , but failed 
//////////////////////////////////// 
void CMedia::SetVolume(int iFactor) 
{ 
	AfxMessageBox("The setVolume() function have some problem in my computer,\r\n\ 
		I can not get the device handle while the media is playing,\r\n\ 
	that is, I can not get the audio divice handle while the handle is opened by my media. \r\n\ 
	   I had used many ways , but failed.\r\n\ 
	   See source code and Please mail me if you can solve the problem. : )\r\n\ 
	   My mail: mdreamchen@solomon-systech.com \r\n\ 
	   Thanks for your kindness."); 
////// method 1////////////////////// 
/*	MMRESULT mmResult; 
	HWAVEOUT hWaveOut; 
//	PCMWAVEFORMAT pFormat; 
	WAVEFORMATEX waveFormat; 
	waveFormat.wFormatTag=WAVE_FORMAT_PCM; 
	waveFormat.nChannels=2; 
	waveFormat.nSamplesPerSec=44.1; 
	waveFormat.wBitsPerSample=16; 
	waveFormat.nBlockAlign=waveFormat.nChannels * waveFormat.wBitsPerSample / 8; 
	waveFormat.nAvgBytesPerSec=waveFormat.nBlockAlign * waveFormat.nSamplesPerSec; 
	waveFormat.cbSize=0; 
	UINT numDevs=waveOutGetNumDevs(); 
	WAVEOUTCAPS waveOutCaps; 
	for(UINT openDevId=0;openDevId < numDevs;openDevId++) 
	{ 
		mmResult=waveOutGetDevCaps(openDevId,&waveOutCaps,sizeof(WAVEOUTCAPS)); 
		//		mmResult=waveOutOpen(&hWaveOut,openDevId,&waveFormat,NULL,NULL,NULL); 
//		if(mmResult==MMSYSERR_NOERROR) 
//			break; 
		//ASSERT(mmResult==MMSYSERR_NOERROR); 
	} 
 
	mmResult=waveOutOpen(&hWaveOut,WAVE_MAPPER ,&waveFormat,NULL,0,WAVE_ALLOWSYNC ); 
	ASSERT(mmResult!=MMSYSERR_BADDEVICEID); 
	ASSERT(mmResult==MMSYSERR_NOERROR); 
	DWORD dwVolume; 
	mmResult=waveOutGetVolume(hWaveOut,&dwVolume); 
	ASSERT(mmResult==MMSYSERR_NOERROR); 
	mmResult=waveOutSetVolume(hWaveOut,0); 
	waveOutClose(hWaveOut); 
*/ 
 
////////method 2///////////////////use a volume class whiched downloaded from net 
/*	IVolume* pWaveVolume=(IVolume*)new CVolumeOutWave(); 
	if(!pWaveVolume || !pWaveVolume->IsAvailable()) 
		return; 
	DWORD dwVolume=pWaveVolume->GetCurrentVolume(); 
*/ 
 
//////////method 3///////////////////use a volume class whiched downloaded from net 
/*	IVolume* pMasterVolume = (IVolume*)new CVolumeOutMaster(); 
	if ( !pMasterVolume || !pMasterVolume->IsAvailable() ) 
	{ 
 		// handle error 
		return; 
	} 
	DWORD dwVolume=pMasterVolume->GetCurrentVolume(); 
*/ 
} 
//SetFullScreen(), 
//Only for media play who has a play window 
bool CMedia::SetFullScreen() 
{ 
	///////this functon can not be effected to the media window 
	////// should close previous media window ,then call this function! 
	if(!m_bIsVideo)			//not video file 
		return FALSE; 
	MCIERROR mciError=mciSendString("window myMedia state minimized",NULL,0,NULL); 
	mciSendString("play myMedia fullscreen",NULL,0,NULL); 
 
#if _DEBUG 
	DispatchMciError(mciError); 
#endif 
	return TRUE; 
} 
 
bool CMedia::IsVideo() 
{ 
	return m_bIsVideo; 
} 
 
// function GetFrameRate() only for video file 
int CMedia::GetFrameRate() 
{ 
	ASSERT(m_bIsVideo); 
	MCIERROR mciError;	 
	char chFrameRate[50]; 
//	mciError=mciSendString("set myMedia time format frames",NULL,0,NULL); 
 	/////// frames format is default ///////////////// 
	mciError=mciSendString("status myMedia frame rate",chFrameRate,sizeof(chFrameRate),NULL); 
#if _DEBUG 
	DispatchMciError(mciError); 
#endif 
	CString strFrameRate; 
	strFrameRate.Format("%s",chFrameRate); 
	int temp=StrToInt((LPCTSTR)strFrameRate)/1000; 
	return temp; 
} 
 
//GetTotalSec(), 
//return media file's total length in seconds 
LONG CMedia::GetTotalSec() 
{ 
	if(m_bIsVideo) //video file 
	{ 
		int frameRate=GetFrameRate(); 
		if(frameRate)	//not failure 
			return GetLength()/frameRate; 
		else		//asf file can not get framerate 
			return 3600; //an hour 
	}else     //music file 
		return GetLength()/1000; 
}