Condividi tramite


Passaggio 7: Arrestare la sessione multimediale

Questo argomento è il passaggio 7 dell'esercitazione Su come riprodurre file multimediali con Media Foundation. Il codice completo viene visualizzato nell'argomento Esempio di riproduzione sessione multimediale.

Per arrestare la sessione multimediale, seguire questa procedura:

  1. Chiamare IMFMediaSession::Close per chiudere la presentazione corrente.
  2. Attendere l'evento MESessionClosed . Questo evento è garantito essere l'ultimo evento della sessione multimediale.
  3. Chiamare FMIMediaSession::Shutdown. Questo metodo causa il rilascio delle risorse delle sessioni multimediali.
  4. Chiamare FMIMediaSource::Shutdown nell'origine multimediale corrente.

Il metodo seguente arresta la sessione multimediale. Usa un handle eventi (m_hCloseEvent) per attendere l'evento MESessionClosed . Vedere Passaggio 5: Gestire gli eventi della sessione multimediale.

//  Close the media session. 
HRESULT CPlayer::CloseSession()
{
    //  The IMFMediaSession::Close method is asynchronous, but the 
    //  CPlayer::CloseSession method waits on the MESessionClosed event.
    //  
    //  MESessionClosed is guaranteed to be the last event that the 
    //  media session fires.

    HRESULT hr = S_OK;

    SafeRelease(&m_pVideoDisplay);

    // First close the media session.
    if (m_pSession)
    {
        DWORD dwWaitResult = 0;

        m_state = Closing;
           
        hr = m_pSession->Close();
        // Wait for the close operation to complete
        if (SUCCEEDED(hr))
        {
            dwWaitResult = WaitForSingleObject(m_hCloseEvent, 5000);
            if (dwWaitResult == WAIT_TIMEOUT)
            {
                assert(FALSE);
            }
            // Now there will be no more events from this session.
        }
    }

    // Complete shutdown operations.
    if (SUCCEEDED(hr))
    {
        // Shut down the media source. (Synchronous operation, no events.)
        if (m_pSource)
        {
            (void)m_pSource->Shutdown();
        }
        // Shut down the media session. (Synchronous operation, no events.)
        if (m_pSession)
        {
            (void)m_pSession->Shutdown();
        }
    }

    SafeRelease(&m_pSource);
    SafeRelease(&m_pSession);
    m_state = Closed;
    return hr;
}

Prima dell'uscita dell'applicazione, arrestare la sessione multimediale e quindi chiamare MFShutdown per arrestare la piattaforma Microsoft Media Foundation.

//  Release all resources held by this object.
HRESULT CPlayer::Shutdown()
{
    // Close the session
    HRESULT hr = CloseSession();

    // Shutdown the Media Foundation platform
    MFShutdown();

    if (m_hCloseEvent)
    {
        CloseHandle(m_hCloseEvent);
        m_hCloseEvent = NULL;
    }

    return hr;
}

Riproduzione di audio/video

Come riprodurre file multimediali con Media Foundation