Windows Media Player Application Sample Code Analysis (Mobile 6.5.3)

The code named WmpSample demonstrates how to use the Windows Media Player ATL control from a Win32 application.

1. 下载示例代码 

您可以从http://msdn.microsoft.com/en-us/library/bb158713.aspx下载,也可以从安装的Windows Mobile 6.5.3 Professional DTK带的例子中“Windows Mobile 6.5.3 DTK\Samples\PocketPC\CPP\ATL\wmpsample” 找到此示例代码。

2. 开发环境准备

My Development Environments


OS Windows 7

SDK: Windows Mobile 6.5.3 Professional DTK

Development Environment: Visual Studio 2008

3. 部署并在模拟器中运行

    3.1 打开解决方案“WMPSample”的属性页,点击“配置管理器”修改部署的平台

修改部署设备为: Windows Mobile 6.5.3 Professional Emulator。如图

clip_image001

    3.2 重新生成解决方案,并运行。运行结果如图

clip_image002

4. 解析其实现播放器的解决方案

   4.1 wmpsample.cpp

        It contains initialization code for the application.

        If PreMessageLoop reutrn OK,then the program continuously runs with RunMessageLoop() and PostMessageLoop().

   4.2 MainWindow.cpp

主要实现的消息处理功能就在此cpp中。

        4.2.1  处理Creat消息

a) initilaize the menu bar

b) initialize WMP window
    AtlAxWinInit();  
    GetClientRect(hwnd, &rcClient);
    VERIFY(SystemParametersInfo(SPI_GETWORKAREA, 0, &rcMainWindow, 0));
    rcClient.bottom -= rcMainWindow.top;

c) create WMP control
    _AtlModule.m_appWindow.m_wmplayer.Create(hwnd, rcClient, TEXT("{6BF52A52-394A-11d3-B153-00C04F79FAA6}"),
                WS_CHILD | WS_VISIBLE | WS_BORDER, 0);
    CBR(_AtlModule.m_appWindow.m_wmplayer.m_hWnd != NULL);

    hr = _AtlModule.m_appWindow.m_wmplayer.QueryControl(&(_AtlModule.m_appWindow.m_spWMPPlayer));
    CHR(hr);

    hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
    CHR(hr);

    spEventListener = pEventListener;       
    hr = _AtlModule.m_appWindow.m_spWMPPlayer->QueryInterface(&spConnectionContainer);
    CHR(hr);

4.2.1  处理文件打开的消息

主要实现的功能:弹出对话框选择文件并播放

a)选择文件

    OPENFILENAMEEX  ofn;

    TCHAR           szFileName[_MAX_PATH]; //打开的文件名

    memset(&ofn, 0, sizeof(ofn)); // initialize structure to 0/NULL
    szFileName[0] = TEXT('\0');
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = ARRAYSIZE(szFileName);
    ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER;
    ofn.lpstrFilter = TEXT("All Media files\0*.wma;*.mp3;*.mpeg;*.avi;*.wmv;*.asf;*.wav;*.mid;*.dvr-ms;*.mid;*.rmi;*.midi;*.wm;*.snd;*.au;*.aif;*.mpg;*.m1v;*.mp2;*.mpa\0");
    ofn.hwndOwner = hwnd;
    ofn.hInstance = _AtlBaseModule.GetResourceInstance();

    CBR(GetOpenFileNameEx(&ofn));

    //关闭选择文件的Dialog
    EndDialog(hwnd, IDOK);

b)播放
    hr = _AtlModule.m_appWindow.m_spWMPPlayer->put_URL(CComBSTR(szFileName));

4.2.3 处理控制文件的播放和暂停的消息

           _AtlModule.m_appWindow.m_spWMPPlayer->get_controls(&spWMPControls);
           if (fPlayPause)
           {
               spWMPControls->pause();
           }
           else
           {
               spWMPControls->play();
           }
           fPlayPause = !fPlayPause;

posted @ 2010-06-10 08:55  Xuesong  阅读(799)  评论(0编辑  收藏  举报