December 22, 2014
Hot Topics:

Video Playbacks on Windows Mobile 5.0: A Guide for Beginners

  • March 15, 2006
  • By Alex Gusev
  • Send Email »
  • More Articles »

A somewhat more sophisticated scenario may listen for events. The previous sample becomes just a little bit more complicated:

#include <dshow.h>

CComPtr<IGraphBuilder>   pGraph;
CComPtr<IMediaControl>   pMediaControl;
CComPtr<IVideoWindow>    pVidWin;
CComPtr<IMediaEvent>     pMediaEvent;

void PlayMediaFile(HWND hwndOwner, LPCTSTR lpszFileName)
{
    HRESULT hr = S_OK;

    // Create the filter graph manager.
    hr = pGraph.CoCreateInstance(CLSID_FilterGraph);
    if ( FAILED(hr) )
    {
       // add some error handling here
       return;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl,
                                (void **)&pMediaControl);
    hr = pGraph->QueryInterface(IID_IVideoWindow,
                                (void **)&pVidWin);
    hr = pGraph->QueryInterface(IID_IMediaEvent,
                                (void **)&pMediaEvent);

    // Build the graph.
    hr = pGraph->RenderFile(lpszFileName, NULL);

    if ( SUCCEEDED(hr) )
    {
       // Set the video window.
       pVidWin->put_Owner((OAHWND)hwndOwner);
       pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);

       RECT rc;
       GetClientRect(hwndOwner, &rc);
       pVidWin->SetWindowPosition(0, 0, rc.right, rc.bottom);

       // Run the graph.
       hr = pMediaControl->Run();
       if ( SUCCEEDED(hr) )
       {
            long lEventCode = 0;
            pMediaEvent->WaitForCompletion(INFINITE,lEventCode);
       }
       else
       {
       // add some error processing here
       }
    }
    else
    {
    // add some error processing here
    }
}

The very next step is to receive filter graph notifications. To achive this, all you need to do is to modify the previous sample as:

// define notification message
#define WM_MEDIA_NOTIFY     WM_USER + 0x1000

// define a global variable or member of some class somewhere
CComPtr<IMediaEventEx> m_pMediaEventEx;
...
void PlayMediaFile(HWND hwndOwner, LPCTSTR lpszFileName)
{

    hr = pGraph->QueryInterface(IID_IMediaEventEx,
                                (void **)&m_pMediaEventEx);
    m_pMediaEventEx->SetNofityWindow((OAHWND)hWndOwner,
                                     WM_MEDIA_NOTIFY,0);
...
}

As a result, your hWndOwner window will get notified by a WM_MEDIA_NOTIFY message, so you can handle it as you want to; for example, as follows in WinProc:

#include <evcode.h>
...
    case WM_MEDIA_NOTIFY:
    {
        if ( m_pMediaEventEx )
        {
            long lEventCode = 0;
            long* pParam1 = NULL, pParam2 = NULL;
            HRESULT hr = S_OK;

            while ( SUCCEEDED(hr = m_pMediaEventEx->GetEvent
                              (&lEventCode,pParam1,pParam2)) )
            {
                m_pMediaEventEx->FreeEventParams(lEventCode,pParam1,
                                                 pParam2);
                switch (lEventCode)
                {
                case EC_COMPLETE:
                    // perform some action, e.g. as following
                    m_pMediaEventEx->Stop();
                    m_pMediaEventEx->WaitForCompletion(INFINITE,
                                                       lEventCode);
                    m_pMediaEventEx->Release();
                    m_pMediaEventEx = NULL;
                    ....
                    break;
                case EC_USERABORT:
                    // perform some action
                    break;
                ...
                }
            }
        }
    }
    break;

or using MFC,

ON_MESSAGE(WM_MEDIA_NOTIFY,OnMediaNotify)
...

Conclusion

The topics in this article give you enough "meat" to build your own simple player. All the surrounding things such as the GUI or error handling were left out to make the story as simple as possible. If I have a chance, I will discuss other streaming related topics—such as video capturing and so forth—in future articles.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. Now, he works at an international retail software company as a team leader of the Mobile R department, making programmers' lives in the mobile jungles a little bit simpler.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel