音楽・音声の再生 (CMediaControlDS, CMediaControlMM)

(2007.05.16)
#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.
 
// '06.08.17 : 新規作成 (sha)
// '06.08.18 : コードの整備 (sha)
// '07.04.06 : コードの整備 (sha)
// '07.05.16 : コードの整備 (sha)
 
#include <strmif.h>                                // IGraphBuilder
#include <control.h>                            // IMediaControl, IMediaEvent
#include <uuids.h>                                // CLSID_FilterGraph
#pragma comment(lib, "strmiids.lib")
 
#include <Mmsystem.h>
#pragma comment(lib, "winmm.lib")
 
_COM_SMARTPTR_TYPEDEF(IGraphBuilder, __uuidof(IGraphBuilder));
_COM_SMARTPTR_TYPEDEF(IMediaControl, __uuidof(IMediaControl));
_COM_SMARTPTR_TYPEDEF(IMediaEvent, __uuidof(IMediaEvent));
 
 
//////////////////////////////////////////////////////////////////////
// CMediaControlDS
 
class CMediaControlDS
{
protected:
    typedef CMediaControlDS                        thisClass;
 
public:
    CMediaControlDS()
    {
    }
    virtual ~CMediaControlDS()
    {
    }
 
public:
    HRESULT PlaySound(LPCTSTR szFileName)
    {
        USES_CONVERSION;
 
        HRESULT hr = S_OK;
 
        // 毎回、新しくオブジェクトを生成する
        // 再生中の場合、停止します
        m_spMediaControl = thisClass::CreateMediaControl();
        if (m_spMediaControl == NULL)
        {
            return E_FAIL;
        }
 
        IGraphBuilderPtr spGraphBuilder = m_spMediaControl;
        if (spGraphBuilder == NULL)
        {
            return E_FAIL;
        }
 
        hr = spGraphBuilder->RenderFile(T2W((LPTSTR)szFileName), NULL);
        if (FAILED(hr))
        {
            return hr;
        }
 
        hr = m_spMediaControl->Run();
        if (FAILED(hr))
        {
            return hr;
        }
 
        return hr;
    }
    HRESULT StopSound()
    {
        HRESULT hr = S_OK;
 
        if (m_spMediaControl == NULL)
        {
            return S_FALSE;
        }
 
        m_spMediaControl = NULL;
 
        return S_OK;
    }
 
protected:
    static IMediaControlPtr CreateMediaControl()
    {
        HRESULT hr = S_OK;
 
        IMediaControlPtr spMediaControl;
        hr = spMediaControl.CreateInstance(CLSID_FilterGraph);
        if (FAILED(hr))
        {
            return NULL;
        }
 
        return spMediaControl;
    }
 
protected:
    IMediaControlPtr m_spMediaControl;
};
 
 
//////////////////////////////////////////////////////////////////////
// CMediaControlMM
 
class CMediaControlMM
{
protected:
    typedef CMediaControlMM                        thisClass;
 
public:
    CMediaControlMM()
    {
    }
    virtual ~CMediaControlMM()
    {
    }
 
public:
    HRESULT PlaySound(LPCTSTR szFileName)
    {
        DWORD dwSound = SND_ASYNC | SND_NODEFAULT | SND_NOWAIT;        // サウンドの種類
        BOOL br = ::PlaySound(szFileName, NULL, dwSound);
        if (br == FALSE)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
        
        return S_OK;
    }
    HRESULT StopSound()
    {
        DWORD dwSound = 0;
        BOOL br = ::PlaySound(NULL, NULL, dwSound);
        if (br == FALSE)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
        
        return S_OK;
    }
};
 
#ifdef _USE_MMAPI
#define CMediaControl                            CMediaControlMM
#else
#define CMediaControl                            CMediaControlDS
#endif
一覧に戻る
© 2003 WAC.com All Right Reserved.