How to Set the Playback Rate on the Media Session

To implement playback functionality such as fast forward and rewind, applications may need to change the playback rate for a media stream. Media Foundation provides the rate control service that the applications must use to set the playback rate dynamically.

Before setting the playback rate, an application should check whether the rate is supported by the media source. For information about querying for supported rates, see How to Determine Supported Rates.

For information about playback rates, see About Rate Control.

To set the playback rate

  1. Call MFGetService to get the rate control object from the Media Session.

    Applications calling MFGetService must ensure the following:

    • The punkObject parameter contains an initialized IMFMediaSession interface pointer.
    • The rate control object received in the ppvObject parameter is released to avoid memory leaks.
  2. Call the IMFRateControl::SetRate method to set the playback rate. After SetRate completes asynchronously, the application receives the MESessionRateChanged event.

Example

The following code shows how to set the playback rate by calling the SetRate method.

///////////////////////////////////////////////////////////////////////
//  Name: SetPlaybackRate
//  Description: 
//      Gets the rate control service from Media Session.
//      Sets the playback rate to the specified rate.
//  Parameter:
//      pMediaSession: [in] Media session object to query.
//      rateRequested: [in] Playback rate to set.
//      bThin: [in] Indicates whether to use thinning.
///////////////////////////////////////////////////////////////////////

HRESULT SetPlaybackRate(
          IMFMediaSession *pMediaSession, 
          float rateRequested, 
          BOOL bThin)
{
    HRESULT hr = S_OK;
    IMFRateControl *pRateControl = NULL;

    // Get the rate control object from the Media Session.
    hr = MFGetService( 
           pMediaSession, 
           MF_RATE_CONTROL_SERVICE, 
           IID_IMFRateControl, 
           (void**) &pRateControl ); 

    // Set the playback rate.
    if(SUCCEEDED(hr))
    {
        hr = pRateControl ->SetRate( bThin, rateRequested); 
    }

    // Clean up.
    SAFE_RELEASE(pRateControl );

    return hr;
}

The application must be stopped or paused before it can make a transition from a negative or zero rate to a positive rate. For information about these states, see How to Control Presentation States.

Media Session

Rate Control

Seeking, Fast Forward, and Reverse Play