MediaPlayer.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // MediaPlayer.cpp: implementation of the CMediaPlayer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MediaPlayer.h"
  6. LPDIRECTSHOWGRAPHBUILDER   CMediaPlayer::m_pGraph=NULL;
  7. LPDIRECTSHOWMEDIACONTROL   CMediaPlayer::m_pMediaControl=NULL;
  8. LPDIRECTSHOWMEDIAPOSITION  CMediaPlayer::m_pMediaPosition=NULL;
  9. int                        CMediaPlayer::m_musicState=MUSIC_NULL;
  10. bool                       CMediaPlayer::m_bActive=true;
  11. REFTIME                    CMediaPlayer::m_tmDuration=0;
  12. int                        CMediaPlayer::m_numUser=0;
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CMediaPlayer::CMediaPlayer()
  17. {
  18. if(m_numUser==0)
  19. {
  20.         if(!Init())
  21. {
  22. m_musicState=MUSIC_ERROR;
  23. m_bActive=false;
  24. }
  25. }
  26. m_numUser++;
  27. }
  28. CMediaPlayer::~CMediaPlayer()
  29. {
  30. m_numUser--;
  31. if(m_numUser==0)
  32. {
  33.     if(m_pGraph!=NULL)
  34. {
  35.     m_pGraph->Release();
  36.      m_pGraph=NULL;
  37. }
  38.     if(m_pMediaControl!=NULL)
  39. {
  40.     m_pMediaControl->Release();
  41.      m_pMediaControl=NULL;
  42. }
  43.     if(m_pMediaPosition!=NULL)
  44. {
  45.     m_pMediaPosition->Release();
  46.      m_pMediaPosition=NULL;
  47. }
  48. m_musicState=MUSIC_NULL;
  49. }
  50. }
  51. bool CMediaPlayer::Init()
  52. {
  53. HRESULT hr;
  54. hr=CoCreateInstance(CLSID_FilterGraph, NULL,
  55.                      CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);
  56. if(FAILED(hr))
  57. {
  58. // MessageBox(NULL, "Unable to CoCreateInstance IID_IGraphBuilder", "ERROR", MB_OK);
  59.         m_pGraph=NULL;
  60. return false;
  61. }
  62. hr=m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
  63. if(FAILED(hr))
  64. {
  65. m_pMediaControl=NULL;
  66. // MessageBox(NULL, "QueryInterface IID_IMediaControl error", "ERROR", MB_OK);
  67. return false;
  68. }
  69. hr=m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
  70. if(FAILED(hr))
  71. {
  72. // MessageBox(NULL, "QueryInterface IID_IMediaPosition error", "ERROR", MB_OK);
  73. m_pMediaPosition=NULL;
  74. return false;
  75. }
  76.     m_musicState=MUSIC_NULL;
  77. return true;
  78. }
  79. bool CMediaPlayer::CreateMusic(char *filename)
  80. {
  81. if(m_musicState==MUSIC_ERROR|| m_musicState!=MUSIC_NULL)return false;
  82. HRESULT hr;
  83. WCHAR wcharStr[MAX_PATH];
  84. //Convert the file name to the string that DirectAudio needs
  85. MultiByteToWideChar(CP_ACP, 0, filename, -1, wcharStr, MAX_PATH);
  86. hr=m_pGraph->RenderFile(wcharStr, NULL);
  87. if(FAILED(hr))
  88. {
  89. MessageBox(NULL, "Load music file err", "ERROR", MB_OK);
  90. return false;
  91. }
  92. m_musicState=MUSIC_IDLE;
  93.     m_pMediaPosition->get_Duration(&m_tmDuration);
  94. return true;
  95. }
  96. void CMediaPlayer::Play()
  97. {
  98. if(m_musicState==MUSIC_ERROR|| 
  99.    m_musicState==MUSIC_NULL )return;
  100. if(!m_bActive)return;
  101. REFTIME curTime;
  102.     m_pMediaPosition->get_CurrentPosition(&curTime);
  103.     m_pMediaPosition->put_CurrentPosition(curTime);
  104. m_pMediaControl->Run();
  105. m_musicState=MUSIC_PLAYING;
  106. }
  107. void CMediaPlayer::Pause()
  108. {
  109. if(m_musicState==MUSIC_ERROR || m_musicState==MUSIC_NULL)return;
  110.     m_pMediaControl->Pause();
  111. m_musicState=MUSIC_PAUSE;
  112. }
  113. void CMediaPlayer::Stop()
  114. {
  115. if(m_musicState==MUSIC_ERROR)return;
  116.     m_pMediaControl->Stop();
  117.     m_pMediaPosition->put_CurrentPosition(0);
  118. m_musicState=MUSIC_IDLE;
  119. }
  120. void CMediaPlayer::SetVolume(int iVolume)
  121. {
  122. if(m_musicState==MUSIC_ERROR)return;
  123.     IBasicAudio* pBA;
  124. HRESULT hrErr = m_pGraph->QueryInterface(IID_IBasicAudio,(void**)&pBA); 
  125. if(FAILED(hrErr)) return ; 
  126. if(iVolume<0)iVolume=0;
  127. if(iVolume>100)iVolume=100;
  128.     iVolume=-6000+iVolume*100;
  129. pBA->put_Volume(iVolume);  
  130. pBA->Release();  
  131. }
  132. int CMediaPlayer::GetRemainTime()
  133. {
  134. if(m_musicState==MUSIC_ERROR)return -1;
  135. REFTIME curTime;
  136.     m_pMediaPosition->get_CurrentPosition(&curTime);
  137.     return  int(m_tmDuration-curTime);
  138. }
  139. bool CMediaPlayer::IsMediaEnable()
  140. {
  141.     if(m_musicState==MUSIC_ERROR)return false;
  142. return true;
  143. }
  144. void CMediaPlayer::SetMediaActive(bool bActive)
  145. {
  146.     m_bActive=bActive;
  147. }
  148. bool CMediaPlayer::IsMediaActive()
  149. {
  150.     return m_bActive;
  151. }