📄 media.cpp
字号:
// Media.cpp: implementation of the CMedia class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CMyMP3.h"
#include "Media.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#pragma comment (lib,"Ole32.lib")
#ifdef CPU_X86EM
#pragma comment (lib,".\lib\X86em\Strmiids.lib")
#pragma comment (lib,".\lib\X86em\Strmbase.lib")
#endif
#ifdef CPU_MIPSII
#pragma comment (lib,".\lib\MIPSII\Strmiids.lib")
#pragma comment (lib,".\lib\MIPSII\Strmbase.lib")
#endif
#ifdef CPU_ARM4I
#pragma comment (lib,".\lib\ARM4I\Strmiids.lib")
#pragma comment (lib,".\lib\ARM4I\Strmbase.lib")
#endif
//========================================================================================================
//----------------------------------------------------------------------------------------------
//Macro define
//Default play mode
#define DEFAULT_DISPLAY_MODE DISP_NATIVE
//----------------------------------------------------------------------
//Initialize
CMedia *CMedia::m_pInstance = NULL;
//------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMedia::CMedia():
m_pGB(NULL),
m_pMC(NULL),
m_pME(NULL),
m_pVW(NULL),
m_pBA(NULL),
m_pBV(NULL),
m_pMS(NULL),
m_hWndVideo(NULL),
m_bExitThrd(TRUE),
m_bThrdRunning(FALSE),
m_DispMode(DEFAULT_DISPLAY_MODE),
m_hWndNotify(NULL),
m_rcDisp(RC_WHOLE_WINDOWN_AREA)
{
memset(m_szFileName,0,sizeof(m_szFileName));
}
CMedia::~CMedia()
{
if(m_pInstance != NULL)
{
delete m_pInstance;
m_pInstance = NULL;
}
}
//------------------------------------------------------------
//Description:
// Play the media file
// When you call the function,you should call Open() before.
//
//-------------------------------------------------------------
BOOL CMedia::Play()
{
// Run the graph to play the media file
if(m_pMC == NULL)
{
return FALSE;
}
return SUCCEEDED(m_pMC->Run());
}
//------------------------------------------------------------
//Description:
// Pause.
// When you call the function,you should call Open() before.
//
//-------------------------------------------------------------
BOOL CMedia::Pause()
{
if(m_pMC == NULL)
{
return FALSE;
}
return SUCCEEDED(m_pMC->Pause());
}
//------------------------------------------------------------
//Description:
// Stop.
// When you call the function,you should call Open() before.
//
//-------------------------------------------------------------
BOOL CMedia::Stop()
{
if(m_pMC == NULL || m_pMS == NULL)
{
return FALSE;
}
HRESULT hr;
hr = m_pMC->Stop();
SetPositionCurrent(0);
return SUCCEEDED(hr);
}
//--------------------------------------------------------------------------
//Description:
// Open the media file. When succeed in calling the function ,
//you should call the Close() to release the resource
//
//-------------------------------------------------------------------------
BOOL CMedia::Open(const TCHAR *pcszFileName)
{
BOOL bResult = FALSE;
if(_tcslen(pcszFileName) >= MAX_PATH)
{
goto END;
}
else
{
_tcscpy(m_szFileName,pcszFileName);
//Check the file existing
HANDLE hdFile = CreateFile(m_szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
if(hdFile == INVALID_HANDLE_VALUE)
{
//The file doesn't exist
goto END;
}
else
{
CloseHandle(hdFile);
}
}
// Initialize COM
if(CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK)
{
goto END;
}
// Get the interface for DirectShow's GraphBuilder
if(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&m_pGB) != S_OK)
{
goto END;
}
// Have the graph construct its the appropriate graph automatically
if(m_pGB->RenderFile(m_szFileName, NULL) != NOERROR)
{
goto END;
}
// QueryInterface for DirectShow interfaces
if(m_pGB->QueryInterface(IID_IMediaControl, (void **)&m_pMC) != NOERROR)
{
goto END;
}
if(m_pGB->QueryInterface(IID_IMediaEventEx, (void **)&m_pME) != NOERROR)
{
goto END;
}
if(m_pGB->QueryInterface(IID_IMediaSeeking, (void **)&m_pMS) != NOERROR)
{
goto END;
}
// Query for video interfaces, which may not be relevant for audio files
if(m_pGB->QueryInterface(IID_IVideoWindow, (void **)&m_pVW) != NOERROR)
{
goto END;
}
if(m_pGB->QueryInterface(IID_IBasicVideo, (void **)&m_pBV) != NOERROR)
{
goto END;
}
if(CheckVisibility() == FALSE)
{
//It just be the audio file, and don't need video filter.
// Relinquish ownership (IMPORTANT!) after hiding
if(m_pVW != NULL)
{
m_pVW->put_Visible(OAFALSE);
m_pVW->put_Owner(NULL);
}
if(m_pBV != NULL)
{
m_pBV->Release();
m_pBV = NULL;
}
if(m_pVW != NULL)
{
m_pVW->Release();
m_pVW = NULL;
}
}
// Query for audio interfaces, which may not be relevant for video-only files
if(m_pGB->QueryInterface(IID_IBasicAudio, (void **)&m_pBA) != NOERROR)
{
goto END;
}
//Set play mode
SetDisplayMode(m_DispMode);
//Get the capabilities of the media file.
m_pMS->GetCapabilities(&m_dwCapability);
bResult = TRUE;
END:
if(bResult == FALSE)
{
//Release the resource
Close();
}
return bResult;
}
//------------------------------------------------------------
//Description:
// This method sets an owning parent for the video window.
//
//Parameters:
// hWnd : [in] Handle of new owner window.
// rcDisp: [in] The display area. If the parameter is not set,
// it will be set as RC_WHOLE_WINDOWN_AREA which means thai
// the whole window is for displaying the video.
//
//----------------------------------------------------------
BOOL CMedia::SetVideoWindow(HWND hWndVideo,const RECT &rcDisp)
{
m_hWndVideo = hWndVideo;
m_rcDisp = rcDisp;
if(m_pVW == NULL)
{
return FALSE;
}
if(FAILED(m_pVW->put_Owner((OAHWND)hWndVideo)))
{
return FALSE;
}
if(FAILED(m_pVW->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN)))
{
return FALSE;
}
//Set play mode in order to notify the displayed area
return SetDisplayMode(m_DispMode);
}
//------------------------------------------------------------
//Description:
// Check the file visibility
// When you call the function,you should call Open() before.
//
//Parameters:
// TRUE: Video
// FALSE: It's not the video
//
//------------------------------------------------------------
BOOL CMedia::CheckVisibility()
{
if (!m_pVW)
{
//No VideoWindow interface. Assuming audio/MIDI file or unsupported video codec
return FALSE;
}
if (!m_pBV)
{
//No BasicVideo interface. Assuming audio/MIDI file or unsupported video codec.
return FALSE;
}
// If this is an audio-only clip, get_Visible() won't work.
//
// Also, if this video is encoded with an unsupported codec,
// we won't see any video, although the audio will work if it is
// of a supported format.
long lVisible;
return SUCCEEDED(m_pVW->get_Visible(&lVisible));
}
//------------------------------------------------------------
//Description:
// Release the resource which opened in the Open()
//
//------------------------------------------------------------
void CMedia::Close()
{
// Relinquish ownership (IMPORTANT!) after hiding
if(m_pVW != NULL)
{
m_pVW->put_Visible(OAFALSE);
m_pVW->put_Owner(NULL);
}
if(m_pMC != NULL)
{
m_pMC->Release();
m_pMC = NULL;
}
if(m_pME != NULL)
{
m_pME->SetNotifyWindow(NULL,NULL,NULL);
m_pME->Release();
m_pME = NULL;
}
if(m_pMS != NULL)
{
m_pMS->Release();
m_pMS = NULL;
}
if(m_pBV != NULL)
{
m_pBV->Release();
m_pBV = NULL;
}
if(m_pBA != NULL)
{
m_pBA->Release();
m_pBA = NULL;
}
if(m_pVW != NULL)
{
m_pVW->Release();
m_pVW = NULL;
}
if(m_pGB != NULL)
{
m_pGB->Release();
m_pGB = NULL;
}
// Finished with COM
memset(m_szFileName,0,sizeof(m_szFileName));
CoUninitialize();
}
//------------------------------------------------------------
//Description:
// Get the instance of object
//
//------------------------------------------------------------
CMedia * CMedia::GetInstance()
{
if(m_pInstance == NULL)
{
m_pInstance = new CMedia();
}
return m_pInstance;
}
//------------------------------------------------------------
//Description:
// Get the media file property.
// When you call the function,you should call Open() before.
//
//------------------------------------------------------------
BOOL CMedia::GetMediaProperty(PMEDIAPROPERTY pOutProperty)
{
MEDIAPROPERTY prop = {0};
if(m_pBA == NULL && m_pBV == NULL && m_pMS == NULL)
{
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -