TNB Library
TnbMciPlayer.h
[詳解]
1#pragma once
11#include "TnbStr.h"
12#include <mmsystem.h>
13#include <digitalv.h>
14#pragma comment(lib, "winmm.lib")
15
16
17
18//TNB Library
19namespace TNB
20{
21
22
23
39{
40public:
41
43 CMciPlayer(void) : m_isPlayed(false), m_lastError(0), m_hParent(NULL), m_wDeviceId(0), m_hViewWnd(NULL), m_isPaused(false)
44 {
45 }
46
49 {
50 Close();
51 }
52
58 void SetParentWnd(HWND hWnd)
59 {
60 m_hParent = hWnd;
61 }
62
68 MCIDEVICEID GetDeviceId() const
69 {
70 return m_wDeviceId;
71 }
72
79 bool OpenMidi(LPCTSTR lpszFileName)
80 {
81 LPCTSTR lpszDeviceType = reinterpret_cast<LPCTSTR>(MCI_DEVTYPE_SEQUENCER);
82 return Open(lpszFileName, lpszDeviceType, true);
83 }
84
91 bool OpenMp3(LPCTSTR lpszFileName)
92 {
93 LPCTSTR lpszDeviceType = _T("MPEGVideo");
94 return Open(lpszFileName, lpszDeviceType, false);
95 }
96
103 bool OpenWave(LPCTSTR lpszFileName)
104 {
105 LPCTSTR lpszDeviceType = reinterpret_cast<LPCTSTR>(MCI_DEVTYPE_WAVEFORM_AUDIO);
106 return Open(lpszFileName, lpszDeviceType, true);
107 }
108
117 bool OpenAvi(LPCTSTR lpszFileName, HWND hViewWnd)
118 {
119 LPCTSTR lpszDeviceType = _T("MPEGVideo"); // _T("avivideo");
120 return Open(lpszFileName, lpszDeviceType, false, hViewWnd);
121 }
122
128 bool Close(void)
129 {
130 bool r = true;
131 if ( m_wDeviceId != 0 )
132 {
133 r = m_Cmd(MCI_CLOSE);
134 if ( r )
135 {
136 m_isPaused = false;
137 m_isPlayed = false;
138 m_wDeviceId = 0;
139 m_hViewWnd = NULL;
140 }
141 }
142 return r;
143 }
144
150 bool IsOpened(void) const
151 {
152 return GetDeviceId() != 0;
153 }
154
162 bool GetLength(DWORD& _ms) const
163 {
164 return m_GetStatus(_ms, MCI_STATUS_LENGTH);
165 }
166
175 DWORD GetMode(void) const
176 {
177 DWORD mode;
178 if ( m_GetStatus(mode, MCI_STATUS_MODE) )
179 {
180 return mode;
181 }
182 return MCI_MODE_NOT_READY;
183 }
184
191 bool Play(void)
192 {
193 MCI_PLAY_PARMS mciPlay = { 0 };
194 bool r = m_Cmd(MCI_PLAY, 0, mciPlay);
195 if ( r )
196 {
197 m_isPlayed = true;
198 m_isPaused = false;
199 }
200 return r;
201 }
202
208 bool Stop(void)
209 {
210 bool r = m_Cmd(MCI_STOP);
211 if ( r )
212 {
213 m_isPlayed = false;
214 }
215 return r;
216 }
217
225 bool Pause(bool isPause = true)
226 {
227 bool r = true;
228 if ( m_isPlayed )
229 {
230 r = m_Cmd(isPause ? MCI_PAUSE : MCI_PLAY);
231 if ( r )
232 {
233 m_isPaused = isPause;
234 }
235 }
236 return r;
237 }
238
244 bool IsPaused(void) const
245 {
246 if ( m_isPlayed && GetMode() == MCI_MODE_PAUSE )
247 {
248 return true;
249 }
250 return false;
251 }
252
260 bool Seek(DWORD pos = 0, bool isResume = true)
261 {
262 if ( pos == 0 )
263 {
264 return m_Cmd(MCI_SEEK, MCI_SEEK_TO_START);
265 }
266 MCI_SEEK_PARMS mciSeek = { 0 };
267 mciSeek.dwTo = pos;
268 bool r = m_Cmd(MCI_SEEK, MCI_TO, mciSeek);
269 if ( r && isResume )
270 {
271 r = m_Cmd(MCI_PLAY);
272 }
273 return r;
274 }
275
282 bool SetVolume(DWORD vol)
283 {
284 MCI_DGV_SETAUDIO_PARMS parm = { 0 };
285 parm.dwItem = MCI_DGV_SETAUDIO_VOLUME;
286 parm.dwValue = vol;
287 return m_Cmd(MCI_SETAUDIO, MCI_DGV_SETAUDIO_ITEM | MCI_DGV_SETAUDIO_VALUE, parm);
288 }
289
297 bool GetPlayingPosition(DWORD& _ms) const
298 {
299 return m_GetStatus(_ms, MCI_STATUS_POSITION);
300 }
301
309 bool SetViewRect(const RECT& rect)
310 {
311 MCI_DGV_PUT_PARMS parms = { 0 };
312 parms.rc = rect;
313 return m_Cmd(MCI_PUT, MCI_DGV_RECT | MCI_DGV_PUT_DESTINATION, parms);
314 }
315
323 bool GetSourceSize(SIZE& _size) const
324 {
325 MCI_ANIM_RECT_PARMS parms = { 0 };
326 if ( m_Cmd(MCI_WHERE, MCI_ANIM_WHERE_SOURCE, parms) )
327 {
328 _size.cx = parms.rc.right - parms.rc.left;
329 _size.cy = parms.rc.bottom - parms.rc.top;
330 return true;
331 }
332 return false;
333 }
334
339 MCIERROR GetLastError(void) const
340 {
341 return m_lastError;
342 }
343
349 static CStr GetErrorString(MCIERROR e)
350 {
351 CStr s;
352 if ( e != MMSYSERR_NOERROR )
353 {
354 ::mciGetErrorString(e, s.GetBuffer(1000), 1000);
355 s.ReleaseBuffer();
356 }
357 return s;
358 }
359
360protected:
361
371 bool Open(LPCTSTR lpszFileName, LPCTSTR lpszDeviceType, bool isDeviceTypeId, HWND hViewWnd = NULL)
372 {
373 Stop();
374 if ( IsOpened() )
375 {
376 Close();
377 }
378 DWORD p1 = MCI_OPEN_TYPE | MCI_OPEN_ELEMENT | (isDeviceTypeId ? MCI_OPEN_TYPE_ID : 0);
379 DWORD p2 = 0;
380 if ( hViewWnd == NULL )
381 {
382 Zero(m_openParms);
383 m_openParms.lpstrDeviceType = lpszDeviceType;
384 m_openParms.lpstrElementName = lpszFileName;
385 p2 = reinterpret_cast<DWORD>(&m_openParms);
386 }
387 else
388 {
389 Zero(m_animOpenParms);
390 m_animOpenParms.hWndParent = hViewWnd;
391 m_animOpenParms.dwStyle = WS_CHILD;
392 m_animOpenParms.lpstrDeviceType = lpszDeviceType;
393 m_animOpenParms.lpstrElementName = lpszFileName;
394 p1 |= (MCI_ANIM_OPEN_PARENT | MCI_ANIM_OPEN_WS);
395 p2 = reinterpret_cast<DWORD>(&m_animOpenParms);
396 }
397 bool r = m_Rst(::mciSendCommand(NULL, MCI_OPEN, p1, p2));
398 if ( r )
399 {
400 m_hViewWnd = hViewWnd;
401 if ( hViewWnd == NULL )
402 {
403 m_wDeviceId = m_openParms.wDeviceID;
404 }
405 else
406 {
407 m_wDeviceId = m_animOpenParms.wDeviceID;
408 }
409 }
410 if ( r )
411 {
412 MCI_SET_PARMS mciSet = { 0 };
413 mciSet.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
414 r = m_Cmd(MCI_SET, MCI_SET_TIME_FORMAT, mciSet);
415 }
416 if ( r && m_hViewWnd != NULL )
417 {
418 RECT rc;
419 ::GetClientRect(m_hViewWnd, &rc);
420 MCI_DGV_PUT_PARMS parms = { 0 };
421 parms.rc = rc;
422 r = m_Cmd(MCI_PUT, MCI_DGV_RECT | MCI_DGV_PUT_DESTINATION, parms);
423 }
424 return r;
425 }
426
431 void SetLastError(MCIERROR e)
432 {
433 m_lastError = e;
434 }
435
436private:
438 bool m_RstFx(MCIERROR r) const
439 {
440 m_lastError = r;
441 return r == 0;
442 }
444 bool m_Rst(MCIERROR r) const
445 {
446 m_lastError = r;
447 if ( r != 0 )
448 {
449 TRACE2("MCIERROR = %d[%s]\n", r, GetErrorString(r));
450 }
451 return r == 0;
452 }
454 template<typename TYP>
455 bool m_Cmd(UINT msg, DWORD param, TYP& typ) const
456 {
457 if ( IsOpened() )
458 {
459 if ( m_hParent != NULL )
460 {
461 typ.dwCallback = reinterpret_cast<DWORD>(m_hParent);
462 param |= MCI_NOTIFY;
463 }
464 return m_Rst(::mciSendCommand(GetDeviceId(), msg, param, reinterpret_cast<DWORD>(&typ)));
465 }
466 return m_RstFx(MCIERR_DEVICE_NOT_READY);
467 }
469 bool m_Cmd(UINT msg, DWORD param = 0) const
470 {
471 MCI_GENERIC_PARMS p = { 0 };
472 return m_Cmd(msg, param, p);
473 }
475 bool m_GetStatus(DWORD& _status, DWORD item) const
476 {
477 MCI_STATUS_PARMS mciStatus = { 0 };
478 mciStatus.dwItem = item;
479 bool r = m_Cmd(MCI_STATUS, MCI_STATUS_ITEM, mciStatus);
480 if ( r )
481 {
482 _status = mciStatus.dwReturn;
483 }
484 return r;
485 }
486 MCI_OPEN_PARMS m_openParms;
487 MCI_ANIM_OPEN_PARMS m_animOpenParms;
488 MCIDEVICEID m_wDeviceId;
489 HWND m_hViewWnd;
490 HWND m_hParent;
491 bool m_isPlayed;
492 bool m_isPaused;
493 mutable MCIERROR m_lastError;
494};
495
496
497
498}; //TNB
文字列管理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
MCI プレイヤークラス.
Definition: TnbMciPlayer.h:39
MCIERROR GetLastError(void) const
[取得] ラストエラーコード取得.
Definition: TnbMciPlayer.h:339
bool Close(void)
[設定] クローズ.
Definition: TnbMciPlayer.h:128
bool GetSourceSize(SIZE &_size) const
[取得] 画像大きさ取得.
Definition: TnbMciPlayer.h:323
void SetParentWnd(HWND hWnd)
[設定] 親ウィンドウ設定.
Definition: TnbMciPlayer.h:58
bool GetPlayingPosition(DWORD &_ms) const
[取得] 演奏位置取得.
Definition: TnbMciPlayer.h:297
bool OpenAvi(LPCTSTR lpszFileName, HWND hViewWnd)
[設定] AVI ファイルオープン.
Definition: TnbMciPlayer.h:117
DWORD GetMode(void) const
[取得] モード取得.
Definition: TnbMciPlayer.h:175
static CStr GetErrorString(MCIERROR e)
[取得] エラー文字列取得.
Definition: TnbMciPlayer.h:349
bool Seek(DWORD pos=0, bool isResume=true)
[設定] シーク.
Definition: TnbMciPlayer.h:260
bool Pause(bool isPause=true)
[設定] 演奏中断・再開.
Definition: TnbMciPlayer.h:225
bool OpenWave(LPCTSTR lpszFileName)
[設定] WAVE ファイルオープン.
Definition: TnbMciPlayer.h:103
bool IsPaused(void) const
[取得] 一時停止中?
Definition: TnbMciPlayer.h:244
bool OpenMidi(LPCTSTR lpszFileName)
[設定] MIDI ファイルオープン.
Definition: TnbMciPlayer.h:79
bool IsOpened(void) const
[取得] オープン確認.
Definition: TnbMciPlayer.h:150
bool GetLength(DWORD &_ms) const
[取得] 全演奏時間.
Definition: TnbMciPlayer.h:162
bool Open(LPCTSTR lpszFileName, LPCTSTR lpszDeviceType, bool isDeviceTypeId, HWND hViewWnd=NULL)
[設定] オープン.
Definition: TnbMciPlayer.h:371
bool SetVolume(DWORD vol)
[設定] 音量設定.
Definition: TnbMciPlayer.h:282
CMciPlayer(void)
コンストラクタ
Definition: TnbMciPlayer.h:43
void SetLastError(MCIERROR e)
[設定] ラストエラーコード設定.
Definition: TnbMciPlayer.h:431
bool Play(void)
[設定] 演奏開始.
Definition: TnbMciPlayer.h:191
MCIDEVICEID GetDeviceId() const
[取得] デバイス ID 取得.
Definition: TnbMciPlayer.h:68
~CMciPlayer(void)
デストラクタ
Definition: TnbMciPlayer.h:48
bool SetViewRect(const RECT &rect)
[設定] 表示RECT設定.
Definition: TnbMciPlayer.h:309
bool Stop(void)
[設定] 演奏停止.
Definition: TnbMciPlayer.h:208
bool OpenMp3(LPCTSTR lpszFileName)
[設定] MP3 ファイルオープン.
Definition: TnbMciPlayer.h:91
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
TNB Library
Definition: TnbDoxyTitle.txt:2