Video Preview

[MFPlay is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions. ]

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.

    if (SUCCEEDED(hr))
        hr = g_pPlayer->CreateMediaItemFromObject(
            TRUE,       // Blocking call.
            0,          // User data.

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

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


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 = NULL;

    if (g_pPlayer)
        g_pPlayer = NULL;


MFPlay requires Windows 7.

Related topics

Video Capture