Edit

Share via


Video Preview

[The feature associated with this page, MFPlay, is a legacy feature. It has been superseded by MediaPlayer and IMFMediaEngine. Those features have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use MediaPlayer and IMFMediaEngine instead of DirectShow, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

This topic describes how to use MFPlay to preview video from a video camera.

MFPlay provides the simplest way in Microsoft Media Foundation to preview video from a capture device. You should use MFPlay if the following conditions are all true:

  • You do not need to capture the video to a file.
  • You do not need fine-grained control over how the video is rendered. (For example, you do not need to control the creation of the Direct3D device.)
  • You do not need to process the video data from the camera prior to rendering.

Otherwise, the Source Reader may be a better option.

To use MFPlay with a video capture device, perform the following steps:

  1. Create a media source for the capture device. See Enumerating Video Capture Devices.
  2. Call MFPCreateMediaPlayer to create an instance of the player object.
  3. Call the IMFPMediaPlayer::CreateMediaItemFromObject method. Pass in a pointer to the IMFMediaSource interface of the media source. The method receives a pointer to the IMFPMediaItem interface.
  4. Call IMFPMediaPlayer::SetMediaItem.
  5. Call IMFPMediaPlayer::Play to begin previewing.

The following code shows these steps:

    IMFPMediaItem *pItem = NULL;

    if (SUCCEEDED(hr))
    {
        hr = MFPCreateMediaPlayer(
            NULL,       // URL.
            FALSE,      // Do not start playback yet.
            0,          // Option flags.
            NULL,       // Callback interface.
            hwnd,
            &g_pPlayer
            );
    }

    if (SUCCEEDED(hr))
    {
        hr = g_pPlayer->CreateMediaItemFromObject(
            g_pSource,
            TRUE,       // Blocking call.
            0,          // User data.
            &pItem
            );
    }

    if (SUCCEEDED(hr))
    {
        hr = g_pPlayer->SetMediaItem(pItem);
    }

    if (SUCCEEDED(hr))
    {
        hr = g_pPlayer->Play();
    }

    SafeRelease(&pItem);

In a typical application, you would provide a pointer to a callback interface in the MFPCreateMediaPlayer function, and use the callback interface to get events from the player. For simplicity, the code shown here skips these steps. For more information, see Getting Started with MFPlay.

Shutting Down

Before the application exits, shut down the media source and the player object as follows:

  1. Call IMFMediaSource::Shutdown on the media source.
  2. Call IMFPMediaPlayer::Shutdown on the player object.
    if (g_pSource)
    {
        g_pSource->Shutdown();
        g_pSource->Release();
        g_pSource = NULL;
    }

    if (g_pPlayer)
    {
        g_pPlayer->Shutdown();
        g_pPlayer->Release();
        g_pPlayer = NULL;
    }

Requirements

MFPlay requires Windows 7.

MFPlay

Video Capture