使用來源解析程式

來源解析程式會接受 URL 或位元組資料流,然後為該內容建立適當的媒體來源。 若要建立來源解析程式,請呼叫 MFCreateSourceResolver。 此函式會傳回 IMFSourceResolver 介面指標。

來源解析程式同時具有同步和非同步方法。 如果您使用來自主要應用程式執行緒的來源解析程式,非同步方法會讓您的使用者介面更具回應性。 同步方法可以封鎖一段明顯的時間,特別是當來源解析程式必須開啟網路資源時。

同步方法包括:

非同步方法包括:

針對非同步方法,每個方法都有對應的 End... 方法來完成非同步要求,以及解除擱置要求 的 Cancel... 方法。 如需 Media Foundation 中非同步方法的詳細資訊,請參閱 非同步回呼方法

下列程式碼範例會從 URL 建立媒體來源。 這個範例會使用同步方法。

//  Create a media source from a URL.
HRESULT CreateMediaSource(PCWSTR sURL, IMFMediaSource **ppSource)
{
    MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID;

    IMFSourceResolver* pSourceResolver = NULL;
    IUnknown* pSource = NULL;

    // Create the source resolver.
    HRESULT hr = MFCreateSourceResolver(&pSourceResolver);
    if (FAILED(hr))
    {
        goto done;
    }

    // Use the source resolver to create the media source.

    // Note: For simplicity this sample uses the synchronous method to create 
    // the media source. However, creating a media source can take a noticeable
    // amount of time, especially for a network source. For a more responsive 
    // UI, use the asynchronous BeginCreateObjectFromURL method.

    hr = pSourceResolver->CreateObjectFromURL(
        sURL,                       // URL of the source.
        MF_RESOLUTION_MEDIASOURCE,  // Create a source object.
        NULL,                       // Optional property store.
        &ObjectType,        // Receives the created object type. 
        &pSource            // Receives a pointer to the media source.
        );
    if (FAILED(hr))
    {
        goto done;
    }

    // Get the IMFMediaSource interface from the media source.
    hr = pSource->QueryInterface(IID_PPV_ARGS(ppSource));

done:
    SafeRelease(&pSourceResolver);
    SafeRelease(&pSource);
    return hr;
}

來源解析程式