BMMP3.cpp
上传用户:cydong117
上传日期:2009-11-10
资源大小:638k
文件大小:6k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. // BMMP3.cpp: implementation of the BMMP3 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CBMMp3::CBMMp3()
  9. {
  10. CoInitialize(NULL);
  11. m_pBasicAudio = NULL;
  12. m_pMediaControl= NULL;
  13. m_pMediaSeeking= NULL;
  14. m_pMediaPosition= NULL;
  15. m_pMediaEventEx= NULL;
  16. m_pGraphBuilder= NULL;
  17. m_bIsPlaying = FALSE; // Is this Mp3 Media playing?
  18. m_bInited = FALSE; // Is this class initialized?
  19. m_bLooping = FALSE; // Is this Mp3 Media Looping Play?
  20. m_hWnd = NULL; // Handle of this class's owner
  21. m_bIsLoadMp3 = FALSE;
  22. }
  23. CBMMp3::~CBMMp3()
  24. {
  25. Release();
  26. CoUninitialize();
  27. }
  28. BOOL CBMMp3::InitBMMp3(HWND hWnd)
  29. {
  30. HRESULT hResult;
  31. m_bInited = TRUE;
  32. m_hWnd = hWnd;
  33. if(SUCCEEDED(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (LPVOID *)&m_pGraphBuilder)))
  34. {
  35. hResult = m_pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl);
  36. if(FAILED(hResult)) 
  37. m_bInited = FALSE;
  38. hResult = m_pGraphBuilder->QueryInterface(IID_IBasicAudio, (void **)&m_pBasicAudio);
  39. if(FAILED(hResult))
  40. m_bInited = FALSE;
  41. hResult = m_pGraphBuilder->QueryInterface(IID_IMediaSeeking,(void **)&m_pMediaSeeking);
  42. if(FAILED(hResult))
  43. m_bInited = FALSE;
  44. hResult = m_pGraphBuilder->QueryInterface(IID_IMediaEventEx,(void **)&m_pMediaEventEx);
  45. if(FAILED(hResult))
  46. m_bInited = FALSE;
  47. hResult = m_pGraphBuilder->QueryInterface(IID_IMediaPosition,(void **)&m_pMediaPosition);
  48. if(FAILED(hResult))
  49. m_bInited = FALSE;
  50. }
  51. else
  52. {
  53. m_bInited = FALSE;
  54. }
  55. return m_bInited;
  56. }
  57. BOOL CBMMp3::LoadMp3(CHAR* szFileName,HWND hWnd)
  58. {
  59. WCHAR wcFileName[MAX_PATH];
  60. if(!m_bInited)
  61. {
  62. if(InitBMMp3(hWnd))
  63. {
  64. MultiByteToWideChar(CP_ACP,0,szFileName, -1,wcFileName,MAX_PATH);
  65. strcpy(m_szFileName,szFileName);
  66. if(SUCCEEDED(m_pGraphBuilder->RenderFile(wcFileName, NULL)))
  67. {
  68. m_bIsLoadMp3 = TRUE;
  69. }
  70. else
  71. {
  72. m_bIsLoadMp3 = FALSE;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. OnStop();
  79. Release();
  80. LoadMp3(szFileName,hWnd);
  81. }
  82. return TRUE;
  83. }
  84. BOOL CBMMp3::OnPlay(BOOL Looping)
  85. {
  86. if(m_bInited&&m_bIsLoadMp3)
  87. {
  88. if(m_bIsPlaying)
  89. {
  90. OnStop();
  91. m_bLooping = Looping;
  92. return OnPlay(Looping);
  93. }
  94. m_bLooping = Looping;
  95. if(FAILED(m_pMediaControl->Run()))
  96. return FALSE;
  97. SetNotifyWindow(WM_DSHOW_NOTIFY,0x00);
  98. m_bIsPlaying = TRUE;
  99. }
  100. else
  101. return FALSE;
  102. return TRUE;
  103. }
  104. BOOL CBMMp3::OnPlay(LONG lVolume,BOOL Looping)
  105. {
  106. if(m_bInited&&m_bIsLoadMp3)
  107. {
  108. if(m_bIsPlaying)
  109. {
  110. OnStop();
  111. m_bLooping = Looping;
  112. return OnPlay(Looping);
  113. }
  114. m_bLooping = Looping;
  115. SetVolume(lVolume);
  116. if(FAILED(m_pMediaControl->Run()))
  117. return FALSE;
  118. SetNotifyWindow(WM_DSHOW_NOTIFY,0x00);
  119. m_bIsPlaying = TRUE;
  120. }
  121. else
  122. return FALSE;
  123. return TRUE;
  124. }
  125. BOOL CBMMp3::OnStop(VOID)
  126. {
  127. if(m_bInited&&m_bIsLoadMp3)
  128. {
  129. if(m_bIsPlaying)
  130. {
  131. SetNotifyFlags(AM_MEDIAEVENT_NONOTIFY); // WM_DSHOW_NOTIFY 甫 惯积矫虐瘤 臼澜
  132. if(FAILED(m_pMediaControl->Stop()))
  133. return FALSE;
  134. m_pMediaPosition->put_CurrentPosition(0); // 贸澜栏肺 倒府钞.
  135. m_bIsPlaying = FALSE;
  136. // m_bLooping = FALSE;
  137. Release();
  138. }
  139. else
  140. return FALSE;
  141. }
  142. return TRUE;
  143. }
  144. BOOL CBMMp3::OnPause(VOID)
  145. {
  146. if(m_bInited&&m_bIsLoadMp3)
  147. {
  148. if(m_bIsPlaying)
  149. {
  150. if(FAILED(m_pMediaControl->Pause()))
  151. return FALSE;
  152. m_bIsPlaying = TRUE;
  153. }
  154. else
  155. return FALSE;
  156. }
  157. return TRUE;
  158. }
  159. HRESULT CBMMp3::MessageProcess(VOID)
  160. {
  161. LONG evCode, evParam1, evParam2;
  162. while(SUCCEEDED(m_pMediaEventEx->GetEvent(&evCode, &evParam1, &evParam2,0)))
  163. {
  164. switch(evCode)
  165. {
  166. case EC_WINDOW_DESTROYED:
  167. OnStop();
  168. break;
  169. case EC_COMPLETE:
  170. if(m_bLooping)
  171. {
  172. LoadMp3(m_szFileName,m_hWnd);
  173. OnPlay(m_bLooping);
  174. return 0l;
  175. }
  176. else 
  177. {
  178. OnStop();
  179. return 0l;
  180. }
  181. break;
  182. case EC_ACTIVATE:
  183. break;
  184. }
  185. m_pMediaEventEx->FreeEventParams(evCode, evParam1, evParam2);
  186. }
  187. return 0l;
  188. }
  189. VOID CBMMp3::Release(VOID)
  190. {
  191. if(m_bIsPlaying)
  192. OnStop();
  193. if(m_pMediaPosition!=NULL)
  194. {
  195. m_pMediaPosition->Release();
  196. m_pMediaPosition = NULL;
  197. }
  198. if(m_pMediaEventEx!=NULL)
  199. {
  200. m_pMediaEventEx->Release();
  201. m_pMediaEventEx = NULL;
  202. }
  203. if(m_pMediaSeeking!=NULL)
  204. {
  205. m_pMediaSeeking->Release();
  206. m_pMediaSeeking = NULL;
  207. }
  208. if(m_pBasicAudio!=NULL)
  209. {
  210. m_pBasicAudio->Release();
  211. m_pBasicAudio = NULL;
  212. }
  213. if(m_pMediaControl!=NULL)
  214. {
  215. m_pMediaControl->Release();
  216. m_pMediaControl = NULL;
  217. }
  218. if(m_pGraphBuilder!=NULL)
  219. {
  220. m_pGraphBuilder->Release();
  221. m_pGraphBuilder= NULL;
  222. }
  223. m_bInited = FALSE;
  224. }
  225. BOOL CBMMp3::SetVolume(LONG lVolume)
  226. {
  227. if(m_bInited&&m_bIsLoadMp3)
  228. {
  229. if(FAILED(m_pBasicAudio->put_Volume(lVolume)))
  230. return FALSE;
  231. }
  232. return TRUE;
  233. }
  234. LONG CBMMp3::GetVolume(VOID)
  235. {
  236. LONG lResult = 0;;
  237. if(m_bInited&&m_bIsLoadMp3)
  238. {
  239. if(m_bIsPlaying)
  240. {
  241. m_pBasicAudio->get_Volume(&lResult);
  242. }
  243. }
  244. return lResult;
  245. }
  246. ////////////////////////////////////////////////////////////////
  247. // Set Message that is Event occur
  248. // lMsg : Long
  249. // cOnOff : BYTE  (ex) On = 0x00     Off = 0x01
  250. /////////////////////////////////////////////////////////////////
  251. BOOL CBMMp3::SetNotifyWindow(LONG lMsg,LONG lOnOff)
  252. {
  253. if(FAILED(m_pMediaEventEx->SetNotifyWindow((OAHWND)m_hWnd, lMsg ,0))) return FALSE;
  254. if(SetNotifyFlags(lOnOff)) return FALSE;
  255. return TRUE;
  256. }
  257. BOOL CBMMp3::SetNotifyFlags(LONG lFlag)
  258. {
  259. if(FAILED(m_pMediaEventEx->SetNotifyFlags(lFlag))) return FALSE;
  260. return TRUE;
  261. }