Condividi tramite


Passaggio 1: Dichiarare la classe CPlayer

Questo argomento è il passaggio 1 dell'esercitazione Come riprodurre file multimediali con Media Foundation. Il codice completo è illustrato nell'argomento Esempio di riproduzione di sessioni multimediali.

In questa esercitazione la funzionalità di riproduzione viene incapsulata nella CPlayer classe . La classe CPlayer viene dichiarata nel modo seguente:

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.
};

Ecco alcuni aspetti da notare su CPlayer:

  • La costante WM_APP_PLAYER_EVENT definisce un messaggio di finestra privata. Questo messaggio viene usato per notificare all'applicazione gli eventi di Sessione multimediale. Vedere Passaggio 5: Gestire gli eventi della sessione multimediale.
  • L'enumerazione PlayerState definisce i possibili stati dell'oggetto CPlayer .
  • La CPlayer classe implementa l'interfaccia IMFAsyncCallback , che viene usata per ottenere le notifiche degli eventi dalla sessione multimediale.
  • Il CPlayer costruttore è privato. L'applicazione chiama il metodo statico CreateInstance per creare un'istanza della CPlayer classe .
  • Il CPlayer distruttore è anche privato. La CPlayer classe implementa IUnknown, quindi la durata dell'oggetto viene controllata tramite il conteggio dei riferimenti (m_nRefCount). Per eliminare definitivamente l'oggetto, l'applicazione chiama IUnknown::Release, non elimina.
  • L'oggetto CPlayer gestisce sia la sessione multimediale che l'origine multimediale.

Implementare IUnknown

La CPlayer classe implementa IMFAsyncCallback, che eredita IUnknown.

Il codice illustrato di seguito è un'implementazione abbastanza standard di IUnknown. Se si preferisce, è possibile usare Active Template Library (ATL) per implementare questi metodi. Tuttavia, CPlayer non supporta CoCreateInstance o nessuna funzionalità COM avanzata, quindi non c'è motivo travolgente di usare ATL qui.

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;
}

Successivo: Passaggio 2: Creare l'oggetto CPlayer

Riproduzione di audio/video

Come riprodurre file multimediali con Media Foundation