步驟 1:宣告 CPlayer 類別

本主題是 如何使用媒體基礎播放媒體檔案教學課程的步驟 1。 完整的程式碼會顯示在 媒體會話播放範例主題中。

在本教學課程中,播放功能會封裝在 類別中 CPlayerCPlayer 類別的宣告方式如下:

const UINT WM_APP_PLAYER_EVENT = WM_APP + 1;   

    // WPARAM = IMFMediaEvent*, WPARAM = MediaEventType

enum PlayerState
{
    Closed = 0,     // No session.
    Ready,          // Session was created, ready to open a file. 
    OpenPending,    // Session is opening a file.
    Started,        // Session is playing a file.
    Paused,         // Session is paused.
    Stopped,        // Session is stopped (ready to play). 
    Closing         // Application has closed the session, but is waiting for MESessionClosed.
};

class CPlayer : public IMFAsyncCallback
{
public:
    static HRESULT CreateInstance(HWND hVideo, HWND hEvent, CPlayer **ppPlayer);

    // IUnknown methods
    STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

    // IMFAsyncCallback methods
    STDMETHODIMP  GetParameters(DWORD*, DWORD*)
    {
        // Implementation of this method is optional.
        return E_NOTIMPL;
    }
    STDMETHODIMP  Invoke(IMFAsyncResult* pAsyncResult);

    // Playback
    HRESULT       OpenURL(const WCHAR *sURL);
    HRESULT       Play();
    HRESULT       Pause();
    HRESULT       Stop();
    HRESULT       Shutdown();
    HRESULT       HandleEvent(UINT_PTR pUnkPtr);
    PlayerState   GetState() const { return m_state; }

    // Video functionality
    HRESULT       Repaint();
    HRESULT       ResizeVideo(WORD width, WORD height);
    
    BOOL          HasVideo() const { return (m_pVideoDisplay != NULL);  }

protected:
    
    // Constructor is private. Use static CreateInstance method to instantiate.
    CPlayer(HWND hVideo, HWND hEvent);

    // Destructor is private. Caller should call Release.
    virtual ~CPlayer(); 

    HRESULT Initialize();
    HRESULT CreateSession();
    HRESULT CloseSession();
    HRESULT StartPlayback();

    // Media event handlers
    virtual HRESULT OnTopologyStatus(IMFMediaEvent *pEvent);
    virtual HRESULT OnPresentationEnded(IMFMediaEvent *pEvent);
    virtual HRESULT OnNewPresentation(IMFMediaEvent *pEvent);

    // Override to handle additional session events.
    virtual HRESULT OnSessionEvent(IMFMediaEvent*, MediaEventType) 
    { 
        return S_OK; 
    }

protected:
    long                    m_nRefCount;        // Reference count.

    IMFMediaSession         *m_pSession;
    IMFMediaSource          *m_pSource;
    IMFVideoDisplayControl  *m_pVideoDisplay;

    HWND                    m_hwndVideo;        // Video window.
    HWND                    m_hwndEvent;        // App window to receive events.
    PlayerState             m_state;            // Current state of the media session.
    HANDLE                  m_hCloseEvent;      // Event to wait on while closing.
};

以下是一些關於 的 CPlayer 注意事項:

  • 數WM_APP_PLAYER_EVENT 定義私人視窗訊息。 此訊息可用來通知應用程式有關媒體會話事件的資訊。 請參閱 步驟 5:處理媒體會話事件
  • 列舉 PlayerState 會定義 物件的可能狀態 CPlayer
  • 類別 CPlayer 會實作 IMFAsyncCallback 介面,用來從媒體會話取得事件通知。
  • CPlayer 構函式是私用的。 應用程式會呼叫靜態 CreateInstance 方法,以建立 類別 CPlayer 的實例。
  • CPlayer 構函式也是私用的。 類別 CPlayer 會實作 IUnknown,因此物件的存留期會透過其參考計數控制, (m_nRefCount) 。 若要終結物件,應用程式會呼叫 IUnknown::Release,而不是 刪除
  • 物件 CPlayer 會同時管理媒體會話和媒體來源。

實作 IUnknown

類別 CPlayer 會實作會繼承IUnknownIMFAsyncCallback

這裡顯示的程式碼是 IUnknown的相當標準實作。 如果您想要的話,您可以使用 Active Template Library (ATL) 來實作這些方法。 不過, CPlayer 不支援 CoCreateInstance 或任何進階的 COM 功能,因此此處沒有使用 ATL 的壓倒性原因。

HRESULT CPlayer::QueryInterface(REFIID riid, void** ppv)
{
    static const QITAB qit[] = 
    {
        QITABENT(CPlayer, IMFAsyncCallback),
        { 0 }
    };
    return QISearch(this, qit, riid, ppv);
}

ULONG CPlayer::AddRef()
{
    return InterlockedIncrement(&m_nRefCount);
}

ULONG CPlayer::Release()
{
    ULONG uCount = InterlockedDecrement(&m_nRefCount);
    if (uCount == 0)
    {
        delete this;
    }
    return uCount;
}

一步:步驟 2:建立 CPlayer 物件

音訊/視訊播放

如何使用媒體基礎播放媒體檔案