Passaggio 2: Creare l'oggetto CPlayer
Questo argomento è il passaggio 2 dell'esercitazione Come riprodurre file multimediali con Media Foundation. Il codice completo è illustrato nell'argomento Esempio di riproduzione di sessioni multimediali.
Per creare un'istanza della CPlayer
classe , l'applicazione chiama il metodo statico CPlayer::CreateInstance
. Questo metodo accetta i parametri seguenti:
- hVideo specifica la finestra da visualizzare.
- hEvent specifica una finestra per la ricezione di eventi. È valido passare lo stesso handle per entrambi gli handle di finestra.
-
ppPlayer riceve un puntatore a una nuova
CPlayer
istanza.
Nel codice seguente viene illustrato il metodo CreateInstance
.
// Static class method to create the CPlayer object.
HRESULT CPlayer::CreateInstance(
HWND hVideo, // Video window.
HWND hEvent, // Window to receive notifications.
CPlayer **ppPlayer) // Receives a pointer to the CPlayer object.
{
if (ppPlayer == NULL)
{
return E_POINTER;
}
CPlayer *pPlayer = new (std::nothrow) CPlayer(hVideo, hEvent);
if (pPlayer == NULL)
{
return E_OUTOFMEMORY;
}
HRESULT hr = pPlayer->Initialize();
if (SUCCEEDED(hr))
{
*ppPlayer = pPlayer;
}
else
{
pPlayer->Release();
}
return hr;
}
HRESULT CPlayer::Initialize()
{
// Start up Media Foundation platform.
HRESULT hr = MFStartup(MF_VERSION);
if (SUCCEEDED(hr))
{
m_hCloseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (m_hCloseEvent == NULL)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
return hr;
}
Il codice seguente mostra il CPlayer
costruttore:
CPlayer::CPlayer(HWND hVideo, HWND hEvent) :
m_pSession(NULL),
m_pSource(NULL),
m_pVideoDisplay(NULL),
m_hwndVideo(hVideo),
m_hwndEvent(hEvent),
m_state(Closed),
m_hCloseEvent(NULL),
m_nRefCount(1)
{
}
Il costruttore esegue le operazioni seguenti:
- Chiama MFStartup per inizializzare la piattaforma Media Foundation.
- Crea un evento di reimpostazione automatica. Questo evento viene utilizzato quando si chiude la sessione multimediale; vedere Passaggio 7: Arrestare la sessione multimediale.
Il distruttore arresta la sessione multimediale, come descritto nel passaggio 7: Arrestare la sessione multimediale.
CPlayer::~CPlayer()
{
assert(m_pSession == NULL);
// If FALSE, the app did not call Shutdown().
// When CPlayer calls IMediaEventGenerator::BeginGetEvent on the
// media session, it causes the media session to hold a reference
// count on the CPlayer.
// This creates a circular reference count between CPlayer and the
// media session. Calling Shutdown breaks the circular reference
// count.
// If CreateInstance fails, the application will not call
// Shutdown. To handle that case, call Shutdown in the destructor.
Shutdown();
}
Si noti che il costruttore e il distruttore sono entrambi metodi di classe protetti. L'applicazione crea l'oggetto usando il metodo statico CreateInstance
. Elimina definitivamente l'oggetto chiamando IUnknown::Release, invece di usare in modo esplicito l'eliminazione.
Passaggio 3: Aprire un file multimediale
Argomenti correlati