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 classe CPlayer
. La classe CPlayer
viene dichiarata come segue:
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. Consulta Passaggio 5: Gestire gli eventi della sessione multimediale.
- L'enumerazione
PlayerState
definisce gli stati possibili dell'oggettoCPlayer
. - La classe
CPlayer
implementa l'interfacciaIMFAsyncCallback, usata per ricevere notifiche degli eventi dalla sessione multimediale. - Il costruttore
CPlayer
è privato. L'applicazione chiama il metodoCreateInstance
statico per creare un'istanza della classeCPlayer
. - Anche il distruttore
CPlayer
è privato. La classeCPlayer
implementa IUnknown, pertanto la durata dell'oggetto viene controllata tramite il relativo conteggio dei riferimenti (m_nRefCount). Per eliminare definitivamente l'oggetto, l'applicazione chiama IUnknown::Release, non eliminare. - L'oggetto
CPlayer
gestisce sia la sessione multimediale che l'origine multimediale.
Implementare IUnknown
La classe CPlayer
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 qualsiasi funzionalità COM avanzata, quindi non c'è motivo eccessivo per 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;
}
Avanti: Passaggio 2: Creare l'oggetto CPlayer
Argomenti correlati