#pragma once
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
// 04.12.30 : コードの微調整 (sha)
// 07.05.14 : コードの整備 (sha)
// 07.05.15 : x64対応 (sha)
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#if _MSC_VER < 1400
#if !defined(_countof)
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#endif
#endif
/////////////////////////////////////////////////////////////////////////////
// CMciDevice
class CMciDevice
{
protected:
typedef CMciDevice thisClass;
public:
CMciDevice()
{
m_nDeviceID = 0;
}
virtual ~CMciDevice()
{
MCIERROR dwResult = this->Close();
if (dwResult != 0)
{
// 無視する。。
}
}
public:
bool IsValid() const
{
return (m_nDeviceID == 0) ? false : true;
}
public:
// デバイスのクローズ
MCIERROR Close()
{
if (!this->IsValid())
{
return !0;
}
DWORD_PTR dwFlags = MCI_WAIT;
MCIERROR dwResult = this->SendCommand(MCI_CLOSE, dwFlags);
if (dwResult != 0)
{
// 無視する。。
}
m_nDeviceID = 0;
return dwResult;
}
public:
MCIERROR SetTimeFormat(DWORD dwTimeFormat)
{
DWORD_PTR dwFlags = MCI_WAIT | MCI_SET_TIME_FORMAT;
MCI_SET_PARMS params = { 0 };
params.dwTimeFormat = dwTimeFormat;
return this->SendCommand(MCI_SET, dwFlags, (DWORD_PTR)¶ms);
}
// MCI_MAKE_TMSF(nTrack, 0, 0, 0)
MCIERROR Play(DWORD dwFrom = 0, DWORD dwTo = 0)
{
DWORD_PTR dwFlags = MCI_WAIT;
MCI_PLAY_PARMS params = { 0 };
if (dwFrom != 0)
{
dwFlags |= MCI_FROM;
params.dwFrom = dwFrom;
}
if (dwTo != 0)
{
dwFlags |= MCI_TO;
params.dwTo = dwTo;
}
return this->SendCommand(MCI_PLAY, dwFlags, (DWORD_PTR)¶ms);
}
MCIERROR Play(HWND hWnd, DWORD dwFrom = 0, DWORD dwTo = 0)
{
DWORD_PTR dwFlags = MCI_NOTIFY;
MCI_PLAY_PARMS params = { 0 };
params.dwCallback = (DWORD)hWnd;
if (dwFrom != 0)
{
dwFlags |= MCI_FROM;
params.dwFrom = dwFrom;
}
if (dwTo != 0)
{
dwFlags |= MCI_TO;
params.dwTo = dwTo;
}
return this->SendCommand(MCI_PLAY, dwFlags, (DWORD_PTR)¶ms);
}
MCIERROR Pause()
{
DWORD_PTR dwFlags = MCI_WAIT;
return this->SendCommand(MCI_PAUSE, dwFlags);
}
MCIERROR Resume()
{
DWORD_PTR dwFlags = MCI_WAIT;
return this->SendCommand(MCI_RESUME, dwFlags);
}
MCIERROR Stop()
{
DWORD_PTR dwFlags = MCI_WAIT;
return this->SendCommand(MCI_STOP, dwFlags);
}
MCIERROR Record()
{
DWORD_PTR dwFlags = MCI_WAIT;
return this->SendCommand(MCI_RECORD, dwFlags);
}
MCIERROR Record(HWND hWnd)
{
DWORD_PTR dwFlags = MCI_NOTIFY;
MCI_RECORD_PARMS params = { 0 };
params.dwCallback = (DWORD)hWnd;
return this->SendCommand(MCI_RECORD, MCI_NOTIFY, (DWORD_PTR)¶ms);
}
MCIERROR Save(LPCTSTR szFileName)
{
DWORD_PTR dwFlags = MCI_WAIT | MCI_SAVE_FILE;
MCI_SAVE_PARMS params = { 0 };
params.lpfilename = szFileName;
return this->SendCommand(MCI_SAVE, dwFlags, (DWORD_PTR)¶ms);
}
public:
MCIERROR SendCommand(UINT uMsg, DWORD_PTR dwParam1 = 0, DWORD_PTR dwParam2 = 0)
{
// MEMO: オープン時には、m_nDeviceIDが0となります
return ::mciSendCommand(m_nDeviceID, uMsg, dwParam1, dwParam2);
}
static CString GetErrorString(MCIERROR err)
{
// MEMO: MCI が返す文字列は、データまたはエラーの説明ですが、最大で 128 文字になる可能性があります。
TCHAR szResult[256] = { 0 };
BOOL br = ::mciGetErrorString(err, szResult, _countof(szResult));
if (br == 0)
{
return _T("");
}
return szResult;
}
public:
MCIDEVICEID m_nDeviceID;
};