Guest User

Untitled

a guest
Apr 20th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.23 KB | None | 0 0
  1. #include "MciPlayer.h"
  2. #include <tchar.h>
  3.  
  4. #define WIN_CLASS_NAME        "CocosDenshionCallbackWnd"
  5. #define BREAK_IF(cond)      if (cond) break;
  6.  
  7. namespace CocosDenshion {
  8.  
  9. static HINSTANCE s_hInstance;
  10. static MCIERROR  s_mciError;
  11.  
  12. LRESULT WINAPI _SoundPlayProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  13.  
  14. MciPlayer::MciPlayer()
  15. : _wnd(NULL)
  16. , _dev(0L)
  17. , _soundID(0)
  18. , _times(0)
  19. , _playing(false)
  20. , strExt("")
  21. {
  22.     if (! s_hInstance)
  23.     {
  24.         s_hInstance = GetModuleHandle( NULL );            // Grab An Instance For Our Window
  25.  
  26.         WNDCLASS  wc;        // Windows Class Structure
  27.  
  28.         // Redraw On Size, And Own DC For Window.
  29.         wc.style          = 0;  
  30.         wc.lpfnWndProc    = _SoundPlayProc;                    // WndProc Handles Messages
  31.         wc.cbClsExtra     = 0;                              // No Extra Window Data
  32.         wc.cbWndExtra     = 0;                                // No Extra Window Data
  33.         wc.hInstance      = s_hInstance;                    // Set The Instance
  34.         wc.hIcon          = 0;                                // Load The Default Icon
  35.         wc.hCursor        = LoadCursor( NULL, IDC_ARROW );    // Load The Arrow Pointer
  36.         wc.hbrBackground  = NULL;                           // No Background Required For GL
  37.         wc.lpszMenuName   = NULL;                           // We Don't Want A Menu
  38.         wc.lpszClassName  = _T(WIN_CLASS_NAME);                 // Set The Class Name
  39.  
  40.         if (! RegisterClass(&wc)
  41.             && 1410 != GetLastError())
  42.         {
  43.             return;
  44.         }
  45.     }
  46.  
  47.     _wnd = CreateWindowEx(
  48.         WS_EX_APPWINDOW,                                    // Extended Style For The Window
  49.         _T(WIN_CLASS_NAME),                                        // Class Name
  50.         NULL,                                        // Window Title
  51.         WS_POPUPWINDOW,/*WS_OVERLAPPEDWINDOW*/               // Defined Window Style
  52.         0, 0,                                                // Window Position
  53.         0,                                                    // Window Width
  54.         0,                                                    // Window Height
  55.         NULL,                                                // No Parent Window
  56.         NULL,                                                // No Menu
  57.         s_hInstance,                                        // Instance
  58.         NULL );
  59.     if (_wnd)
  60.     {
  61.         SetWindowLongPtr(_wnd, GWLP_USERDATA, (LONG_PTR)this);
  62.     }
  63. }
  64.  
  65. MciPlayer::~MciPlayer()
  66. {
  67.     Close();
  68.     DestroyWindow(_wnd);
  69. }
  70.  
  71. void MciPlayer::Open(const char* pFileName, UINT uId)
  72. {
  73. //     WCHAR * pBuf = NULL;
  74.     do
  75.     {
  76.         BREAK_IF(! pFileName || ! _wnd);
  77.         int nLen = (int)strlen(pFileName);
  78.         BREAK_IF(! nLen);
  79. //         pBuf = new WCHAR[nLen + 1];
  80. //         BREAK_IF(! pBuf);
  81. //         MultiByteToWideChar(CP_ACP, 0, pFileName, nLen + 1, pBuf, nLen + 1);
  82.        
  83.         std::string strFile(pFileName);
  84.         int nPos = strFile.rfind(".") + 1;
  85.         strExt = strFile.substr(nPos, strFile.length() - nPos);
  86.  
  87.         Close();
  88.  
  89.         MCI_OPEN_PARMS mciOpen = {0};
  90.         MCIERROR mciError;
  91.         mciOpen.lpstrDeviceType = (LPCTSTR)MCI_ALL_DEVICE_ID;
  92.         WCHAR* fileNameWideChar = new WCHAR[nLen + 1];
  93.         BREAK_IF(! fileNameWideChar);
  94.         MultiByteToWideChar(CP_ACP, 0, pFileName, nLen + 1, fileNameWideChar, nLen + 1);
  95.         mciOpen.lpstrElementName = fileNameWideChar;
  96.  
  97.         mciError = mciSendCommand(0,MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen));
  98.         CC_SAFE_DELETE_ARRAY(mciOpen.lpstrElementName);
  99.         BREAK_IF(mciError);
  100.  
  101.         _dev = mciOpen.wDeviceID;
  102.         _soundID = uId;
  103.         _playing = false;
  104.     } while (0);
  105. }
  106.  
  107. void MciPlayer::Play(UINT uTimes /* = 1 */)
  108. {
  109.     if (! _dev)
  110.     {
  111.         return;
  112.     }
  113.  
  114.     MCI_PLAY_PARMS mciPlay = {0};
  115.     mciPlay.dwCallback = reinterpret_cast<DWORD_PTR>(_wnd);
  116.     s_mciError = mciSendCommand(_dev,MCI_PLAY, MCI_FROM|MCI_NOTIFY,reinterpret_cast<DWORD_PTR>(&mciPlay));
  117.     if (! s_mciError)
  118.     {
  119.         _playing = true;
  120.         _times = uTimes;
  121.     }
  122.  
  123. }
  124. //me
  125. void MciPlayer::SetPitch(DWORD dwSpeed)
  126. {
  127.     // This function was added by me to change pitch of an existing sound id while playing. (Like open AL pitch value).
  128.  
  129. }
  130. //end me
  131. void MciPlayer::Close()
  132. {
  133.     if (_playing)
  134.     {
  135.         Stop();
  136.     }
  137.     if (_dev)
  138.     {
  139.          _SendGenericCommand(MCI_CLOSE);
  140.     }
  141.     _dev = 0;
  142.     _playing = false;
  143. }
  144.  
  145. void MciPlayer::Pause()
  146. {
  147.     _SendGenericCommand(MCI_PAUSE);
  148. }
  149.  
  150. void MciPlayer::Resume()
  151. {
  152.     if (strExt == "mid" || strExt == "MID")
  153.     {
  154.         // midi not supprt MCI_RESUME, should get the position and use MCI_FROM
  155.         MCI_STATUS_PARMS mciStatusParms;
  156.         MCI_PLAY_PARMS   mciPlayParms;  
  157.         mciStatusParms.dwItem = MCI_STATUS_POSITION;  
  158.         _SendGenericCommand(MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms)); // MCI_STATUS  
  159.         mciPlayParms.dwFrom = mciStatusParms.dwReturn;  // get position  
  160.         _SendGenericCommand(MCI_PLAY, MCI_FROM, reinterpret_cast<DWORD_PTR>(&mciPlayParms)); // MCI_FROM
  161.     }
  162.     else
  163.     {
  164.         _SendGenericCommand(MCI_RESUME);
  165.     }  
  166. }
  167.  
  168. void MciPlayer::Stop()
  169. {
  170.     _SendGenericCommand(MCI_STOP);
  171.     _playing = false;
  172.     _times = 0;
  173. }
  174.  
  175. void MciPlayer::Rewind()
  176. {
  177.     if (! _dev)
  178.     {
  179.         return;
  180.     }
  181.     mciSendCommand(_dev, MCI_SEEK, MCI_SEEK_TO_START, 0);
  182.  
  183.     MCI_PLAY_PARMS mciPlay = {0};
  184.     mciPlay.dwCallback = reinterpret_cast<DWORD_PTR>(_wnd);
  185.     _playing = mciSendCommand(_dev, MCI_PLAY, MCI_NOTIFY,reinterpret_cast<DWORD_PTR>(&mciPlay)) ? false : true;
  186. }
  187.  
  188. bool MciPlayer::IsPlaying()
  189. {
  190.     return _playing;
  191. }
  192.  
  193. UINT MciPlayer::GetSoundID()
  194. {
  195.     return _soundID;
  196. }
  197.  
  198. //////////////////////////////////////////////////////////////////////////
  199. // private member
  200. //////////////////////////////////////////////////////////////////////////
  201. void MciPlayer::_SendGenericCommand( int nCommand, DWORD_PTR param1 /*= 0*/, DWORD_PTR parma2 /*= 0*/ )
  202. {
  203.     if (! _dev)
  204.     {
  205.         return;
  206.     }
  207.     mciSendCommand(_dev, nCommand, param1, parma2);
  208. }
  209.  
  210.  
  211. //////////////////////////////////////////////////////////////////////////
  212. // static function
  213. //////////////////////////////////////////////////////////////////////////
  214.  
  215. LRESULT WINAPI _SoundPlayProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  216. {
  217.     MciPlayer * pPlayer = NULL;
  218.     if (MM_MCINOTIFY == Msg
  219.         && MCI_NOTIFY_SUCCESSFUL == wParam
  220.         &&(pPlayer = (MciPlayer *)GetWindowLongPtr(hWnd, GWLP_USERDATA)))
  221.     {
  222.         if (pPlayer->_times)
  223.         {
  224.             --pPlayer->_times;
  225.         }
  226.  
  227.         if (pPlayer->_times)
  228.         {
  229.             mciSendCommand(lParam, MCI_SEEK, MCI_SEEK_TO_START, 0);
  230.  
  231.             MCI_PLAY_PARMS mciPlay = {0};
  232.             mciPlay.dwCallback = reinterpret_cast<DWORD_PTR>(hWnd);
  233.             mciSendCommand(lParam, MCI_PLAY, MCI_NOTIFY,reinterpret_cast<DWORD_PTR>(&mciPlay));
  234.         }
  235.         else
  236.         {
  237.             pPlayer->_playing = false;
  238.         }
  239.         return 0;
  240.     }
  241.     return DefWindowProc(hWnd, Msg, wParam, lParam);
  242. }
  243.  
  244. } // end of namespace CocosDenshion
Advertisement
Add Comment
Please, Sign In to add comment