⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game_sound.cpp

📁 我做的毕业设计
💻 CPP
字号:
#include "Game_User.h"
CSound::CSound(HWND hWnd)
{
	m_pGraph						= NULL;
	m_pMediaControl					= NULL;
	m_pMediaPosition				= NULL;
	m_pEvent						= NULL;
	m_pVW							= NULL;
	m_Format						= Unknown;
	m_hWnd							= hWnd;
	InitSound();
}
CSound::~CSound()
{
	SAFE_RELEASE(m_pMediaPosition);
	SAFE_RELEASE(m_pMediaControl);
	SAFE_RELEASE(m_pEvent);
	SAFE_RELEASE(m_pGraph);
	m_pVW->put_Visible(OAFALSE);
	SAFE_RELEASE(m_pVW);
	//停止使用COM
	CoUninitialize();
}	
HRESULT CSound::InitSound()
{
	//初始化COM
	CoInitialize(NULL);

	//创建Graph对象
	if( FAILED(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, 
		(void**)&m_pGraph)) )
	{
		return E_FAIL;
	}
	m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
	m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
	m_pGraph->QueryInterface(IID_IMediaEvent, (void**)&m_pEvent);
	m_pGraph->QueryInterface(IID_IVideoWindow, (void**)&m_pVW);
	return S_OK;
}
bool CSound::Load(LPCSTR szFileName)
{
	CHAR strSoundPath[MAX_PATH];
	WCHAR wstrSoundPath[MAX_PATH];
	switch(m_Format)
	{
	case MP3:
		//得到程序所在路径
		GetCurrentDirectory(MAX_PATH, strSoundPath);

		//设置音乐所在路径
		strcat(strSoundPath, "\\Music\\");
		strcat(strSoundPath, szFileName);

		/*变为宽字节,防止中文路径有错误*/
		MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH);	
		m_pGraph->RenderFile(wstrSoundPath, NULL);		
		break;
	case AVI:
		//得到程序所在路径
		GetCurrentDirectory(MAX_PATH, strSoundPath);

		//设置音乐所在路径
		strcat(strSoundPath, "\\Music\\");
		strcat(strSoundPath, szFileName);

		/*变为宽字节,防止中文路径有错误*/
		MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH);	
		m_pGraph->RenderFile(wstrSoundPath, NULL);	
		break;
	}
	return true;
}
bool CSound::AVIIsEnd()
{
	REFTIME refPosition;
	REFTIME refDuration;
	switch(m_Format)
	{
	case AVI:
	case MP3:
		m_pMediaPosition->get_CurrentPosition( &refPosition );						// 得到当前位置和总长度
		m_pMediaPosition->get_Duration( &refDuration );
		if( !m_isLoop && ( refPosition >= refDuration ) )							// 判断是否播放完
		{			
			m_pMediaControl->Stop();
			m_Format = Unknown;
			return true;			
		}
		break;
	}
	return false;
}
void CSound::ReplayMp3()                                    //循环播放
{
	REFTIME refPosition;
	REFTIME refDuration;
	m_pMediaPosition->get_CurrentPosition(&refPosition);
	m_pMediaPosition->get_Duration(&refDuration);
	if(!(refPosition < refDuration))
	{
		m_pMediaPosition->put_CurrentPosition(0);
	}

}
HRESULT CSound::Play(LPCSTR szFileName, Format id)
{
	m_Format = id;
	m_isLoop = false;
	if(!Load(szFileName))
	{
		return E_FAIL;
	}
	switch(id)
	{
	case MP3:
		m_pMediaPosition->put_CurrentPosition(0);									// 初始位置		
		m_pMediaControl->Run();														// 播放
		break;
	case AVI:
		m_pMediaPosition->put_CurrentPosition(0);									//设置当前位置
		if(m_pVW != NULL )
			m_pVW->put_Owner((OAHWND)m_hWnd);									//把视频窗口装载到m_hwnd 
		m_pVW->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS| WS_CLIPCHILDREN);

		RECT rect;
		GetClientRect(m_hWnd,&rect);
		m_pVW->SetWindowPosition(rect.left, rect.top, rect.right, rect.bottom);
		m_pVW->put_Visible(OATRUE);											//改变窗口的可见度
		if(m_pVW != NULL )
			m_pVW->put_MessageDrain((OAHWND)m_hWnd);
		m_pMediaControl->Run();
		break;
	}
	return true;
}
void CSound::Stop()											//播放结束
{
	switch(m_Format)
	{
	case AVI:
	case MP3:
		m_pMediaControl->Stop();					// 结束播放
		m_Format = Unknown;		
		break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -