To Retrieve Media Samples with the Synchronous Reader
[The feature associated with this page, Windows Media Format 11 SDK, is a legacy feature. It has been superseded by Source Reader and Sink Writer. Source Reader and Sink Writer have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use Source Reader and Sink Writer instead of Windows Media Format 11 SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]
You must request each sample one at a time from the synchronous reader. This gives you more control over the samples you receive and when you receive them.
Use the IWMSyncReader::GetNextSample method to retrieve a sample. You need to pass mostly pointers to variables that will be filled with information about the sample retrieved as parameters. The only input parameter is wStreamNum. If you pass a stream number, GetNextSample will retrieve the next sample with the specified stream number. If you pass zero for wStreamNum, the next sample that occurs chronologically in the file is retrieved.
By default, the synchronous reader retrieves all of the samples from the outputs in a file in chronological order. If you call GetNextSample and there are no more samples to get, it will return NS_E_NO_MORE_SAMPLES, which is a failed error code. When coding therefore, you can simply loop through samples until the method fails.
Note
To ensure that the synchronous reader delivers correct sample durations for video streams, you must first configure the stream output. Call the IWMSyncReader::SetOutputSetting method to set the g_wszVideoSampleDurations setting to TRUE.
Example Code
The following example code shows how to use GetNextSample to retrieve all samples in a file.
// Loop through all the samples in the file.
do
{
// Get the next sample.
hr = pSyncReader->GetNextSample(0,
&pMyBuffer,
&cnsSampleTime,
&cnsSampleDuration,
&dwFlags,
&dwOutputNumber,
NULL);
if(SUCCEEDED(hr))
{
// TODO: Process the sample in whatever way is appropriate
// to your application. When finished, clean up.
pMyBuffer->Release();
pMyBuffer = NULL;
cnsSampleTime = 0;
cnsSampleDuration = 0;
dwFlags = 0;
dwOutputNumber = 0;
}
}
while (SUCCEEDED(hr));
Related topics