다음을 통해 공유


3단계: 미디어 파일 열기

이 항목은 Media Foundation을 사용하여 미디어 파일을 재생하는 방법 자습서의 3단계입니다. 전체 코드는 미디어 세션 재생 예제 항목에 나와 있습니다.

메서드는 CPlayer::OpenURL URL에서 미디어 파일을 엽니다.

//  Open a URL for playback.
HRESULT CPlayer::OpenURL(const WCHAR *sURL)
{
    // 1. Create a new media session.
    // 2. Create the media source.
    // 3. Create the topology.
    // 4. Queue the topology [asynchronous]
    // 5. Start playback [asynchronous - does not happen in this method.]

    IMFTopology *pTopology = NULL;
    IMFPresentationDescriptor* pSourcePD = NULL;

    // Create the media session.
    HRESULT hr = CreateSession();
    if (FAILED(hr))
    {
        goto done;
    }

    // Create the media source.
    hr = CreateMediaSource(sURL, &m_pSource);
    if (FAILED(hr))
    {
        goto done;
    }

    // Create the presentation descriptor for the media source.
    hr = m_pSource->CreatePresentationDescriptor(&pSourcePD);
    if (FAILED(hr))
    {
        goto done;
    }

    // Create a partial topology.
    hr = CreatePlaybackTopology(m_pSource, pSourcePD, m_hwndVideo, &pTopology);
    if (FAILED(hr))
    {
        goto done;
    }

    // Set the topology on the media session.
    hr = m_pSession->SetTopology(0, pTopology);
    if (FAILED(hr))
    {
        goto done;
    }

    m_state = OpenPending;

    // If SetTopology succeeds, the media session will queue an 
    // MESessionTopologySet event.

done:
    if (FAILED(hr))
    {
        m_state = Closed;
    }

    SafeRelease(&pSourcePD);
    SafeRelease(&pTopology);
    return hr;
}

이 메서드는 다음 단계를 수행합니다.

  1. CPlayer::CreateSession을 호출하여 미디어 세션의 새 instance 만듭니다. 4단계: 미디어 세션 만들기를 참조하세요.
  2. URL에서 미디어 원본을 만듭니다. 이 단계의 전체 코드는 원본 해결 프로그램 사용 항목에 나와 있습니다.
  3. IMFMediaSource::CreatePresentationDescriptor를 호출하여 미디어 원본의 프레젠테이션 설명자를 가져옵니다. 프레젠테이션 설명자는 원본 파일의 각 스트림을 설명합니다.
  4. 재생 토폴로지를 만듭니다. 이 단계의 코드는 재생 토폴로지 만들기 항목에 나와 있습니다.
  5. IMFMediaSession::SetTopology를 호출하여 미디어 세션에서 토폴로지를 설정합니다.

SetTopology 메서드는 비동기적으로 완료됩니다. 완료되면 CPlayer 개체의 IMFAsyncCallback::Invoke 메서드가 호출됩니다. 5단계: 미디어 세션 이벤트 처리를 참조하세요.

다음: 4단계: 미디어 세션 만들기

오디오/비디오 재생

Media Foundation을 사용하여 미디어 파일을 재생하는 방법