Ig_M
Ig_M

Reputation: 160

How to play mp3 that is downloaded at the same time?

I wrote program that downloads mp3 file. It's works fine. My next task is to play the file that is downloaded at the same time using DirectShow. On MSDN website I found and copied this code:

HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        //
    }
IGraphBuilder *pGraph;
hr = CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);

IMediaControl *pControl;
IMediaEvent   *pEvent;
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

hr = pGraph->RenderFile(L"C:\\Test.mp3", NULL);

hr = pControl->Run();

long evCode = 0;
pEvent->WaitForCompletion(INFINITE, &evCode);

pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();

And the problem is that program can not play this file. For writing data I open the file using the following code:

TFileStream *MyFile = new TFileStream(path,fmOpenWrite | fmShareDenyNone);

that allows other applications to read the file. I also used the WINAPI function to open and write the file, but result is the same.

Where I made the mistake?

Please help me - what are the ways to play the file that is downloaded at this time?

Upvotes: 1

Views: 219

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

Standard DirectShow playback assumes the data is taken from a file: File Source (Async) Filter or WM ASF Reader Filter take file path and stream data further. Since your download is still in progress, you cannot provide a complete file and these components cannot play data, because of incompleteness and/or because of sharing violation.

To make the file playable you might want to implement a custom source filter which streams data from internal buffer. If data is not yet available, such filter would synchronize with download and serve file reading request as soon as data is arrived. A custom filter built this way would replace File Source (Async) Filter on the pipeline, with the rest of pipeline built using the same filters.

Windows SDK Async Filter Sample (\Samples\multimedia\directshow\filters\async) might be a good starting point for such custom filter.

Upvotes: 2

Related Questions