Wave.cpp
上传用户:sdpcwz
上传日期:2009-12-14
资源大小:1237k
文件大小:2k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // Wave.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PlayWave.h"
  5. #include "Wave.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CWave
  13. CWave::CWave() 
  14. m_nDeviceID=0; 
  15. m_nElementID=0;
  16. CWave::~CWave() 
  17. if(m_nElementID)
  18. Stop(); 
  19. if(m_nDeviceID) 
  20. CloseDevice(); 
  21. DWORD CWave::OpenDevice() 
  22. DWORD dwResult=0; 
  23. if ( !m_nDeviceID )
  24. {
  25. MCI_OPEN_PARMS mciOpenParms; 
  26. mciOpenParms.lpstrDeviceType=(LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO; 
  27. dwResult=mciSendCommand(NULL,MCI_OPEN, 
  28. MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT, 
  29. (DWORD)(LPVOID)&mciOpenParms); 
  30. m_nDeviceID=mciOpenParms.wDeviceID; 
  31. if(dwResult) 
  32. DisplayErrorMsg(dwResult); 
  33. return dwResult; 
  34. DWORD CWave::CloseDevice() 
  35. DWORD dwResult=0; 
  36. if(m_nDeviceID) 
  37. {
  38. dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL); 
  39. if(dwResult) 
  40. DisplayErrorMsg(dwResult);
  41. else
  42. m_nDeviceID=0; 
  43. return dwResult; 
  44. DWORD CWave::Play(CWnd* pWnd,LPCSTR pFileName) 
  45. MCI_OPEN_PARMS mciOpenParms; 
  46. memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS)); 
  47. mciOpenParms.lpstrElementName=pFileName; 
  48. DWORD dwResult=mciSendCommand(m_nDeviceID,MCI_OPEN, 
  49. MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms); 
  50. if(dwResult) 
  51. DisplayErrorMsg(dwResult);
  52. else
  53. {
  54. m_nElementID=mciOpenParms.wDeviceID; 
  55. MCI_PLAY_PARMS mciPlayParms; 
  56. mciPlayParms.dwCallback=(DWORD)pWnd->GetSafeHwnd();
  57. dwResult=mciSendCommand(m_nElementID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms); 
  58. if(dwResult) 
  59. DisplayErrorMsg(dwResult); 
  60. Stop();
  61. }
  62. return dwResult; 
  63. DWORD CWave::Stop() 
  64. {
  65. DWORD dwResult=0;
  66. if(m_nElementID) 
  67. dwResult=mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL); 
  68. if(dwResult) 
  69. DisplayErrorMsg(dwResult);  
  70. else 
  71. m_nElementID=0; 
  72. return dwResult; 
  73. void CWave::DisplayErrorMsg(DWORD dwError) 
  74. if(dwError) 
  75. {
  76. char szErrorMsg[MAXERRORLENGTH]; 
  77. if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg))) 
  78. strcpy(szErrorMsg,"Unknown Error"); 
  79. AfxMessageBox(szErrorMsg);