October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using the Windows Media Player Control on Handheld Devices

  • October 21, 2005
  • By Alex Gusev
  • Send Email »
  • More Articles »

Using WMP OCX from a Web Application

In case you are using a Web browser, hosting WMP is the easiest thing (this is a SDK sample):

<HTML>
<HEAD>
</HEAD>
<BODY>
   <OBJECT>
      ID=wmpocx
      WIDTH=200
      HEIGHT=150
      CLASSID="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"
      TYPE="application/x-oleobject"
      VIEWASTEXT>
      <PARAM name="uimode" value="none">
   </OBJECT>
<BR>
<script for="wmpocx" event="PlayStateChange(NewState)"
                     language="JScript">ClipPlayState(NewState);
</script>
<script for="wmpocx" event="Error()"
                     language="JScript">StopPlayer();
</script>
<p>
<a href=# OnClick='PlayClip("\storage card\webapp\glass.wmv",
                            ImgVideoPlay, true)'>
   <IMG id="ImgVideoPlay" src="bt_play.gif" border="0"></a> Video
<br>
<a href=# OnClick='PlayClip("\storage card\webapp\jeanne.wma",
                            ImgAudioPlay, false)'>
   <IMG id="ImgAudioPlay" src="bt_play.gif" border="0"></a> Audio
<br>
Play state sequence
<br>
<input type="text" id="PlayStateSequence" width=30>
<SCRIPT language="JScript">
<!--
var CurrentPlayImage = null;
var bVideo = null;
var bWasBuffering = false;
function StopPlayer()
{
   wmpocx.controls.stop();
   wmpocx.close();
   if (CurrentPlayImage != null)
   {
      CurrentPlayImage.src = "bt_play.gif";
   }
   bWasBufferring = false;
}
function ClipPlayState(NewState)
{
   PlayStateSequence.value = PlayStateSequence.value + NewState
                           + " ";
   switch(NewState)
   {
   case 1:    // stopped
   if (bWasBuffering)
   {
      bWasBufferring = false;
      if (CurrentPlayImage != null)
      {
         CurrentPlayImage.src = "bt_play.gif";
      }
   }
      break;
   case 6:    // buffering
      bWasBufferring = true;
      if (CurrentPlayImage != null)
      {
         CurrentPlayImage.src = "bt_load.gif";
      }
      break;
   case 9:     // transitioning
   case 11:    // reconnecting
      bWasBufferring = false;
      break;
   case 3:    // playing
      if (bWasBufferring)
      {
         if (CurrentPlayImage != null)
         {
            CurrentPlayImage.src = "bt_stop.gif";
         }
      if (bVideo)
         {
            wmpocx.fullScreen = true;
         }
      }
      break;
   default:
   }
}
function PlayClip(url, img, video)
{
   if (wmpocx.playState == 3 && bVideo != null && bVideo != video)
   {
      return;
   }
   bVideo = video;
   CurrentPlayImage = img;
   if (wmpocx.playState == 3)
   {
      StopPlayer();
   }
   else
   {
      PlayStateSequence.value = "";
      if (CurrentPlayImage != null)
      {
            CurrentPlayImage.src = "bt_load.gif";
      }
      wmpocx.URL = url;
   }
}
-->
</SCRIPT>
</BODY>
</HTML>
Obviously, you can decorate this HTML as you want to.

Working with Older Versions of the WMP Control

If you have a handheld device without WMP 10, this is yet not the end of the world. You still have an opportunity to use WMP OCX of version 8 for Pocket PC, which has much fewer amazing features, but can serve your needs as well. For exercise purposes, I've created a simple project to illustrate how it works in an MFC environment. The small code snippet below proves that it is pretty similar to the ATL way:

BOOL CWMP8SampleDlg::OnInitDialog()
{
   CDialog::OnInitDialog();
   // Set the icon for this dialog. The framework does this
   // automatically when the application's main window is not
   // a dialog
   SetIcon(m_hIcon, TRUE);     // Set big icon
   SetIcon(m_hIcon, FALSE);    // Set small icon
   CenterWindow(GetDesktopWindow());    // center to the hpc screen
   CRect rect;
   m_Panel.GetClientRect(&rect);
   if ( m_PlayerWnd.CreateControl(__uuidof(WMP),L"",
                                  WS_VISIBLE|WS_CHILD,rect,
                                  &m_Panel,AFX_IDW_PANE_FIRST) )
   {
      LPUNKNOWN lpUnk = m_PlayerWnd.GetControlUnknown();
      HRESULT hr = lpUnk->QueryInterface(__uuidof(IWMP),(void**)
                                         &m_spWMPPlayer);
   }
   else
   {
      AfxMessageBox(L"Failed to create WMP control");
      ::PostQuitMessage(0);
      return 0;
   }
   if ( m_spWMPPlayer )
   {
      m_WMPEvents.m_pMainDlg = (CWMP8SampleDlg*)this;
      CComPtr<IConnectionPointContainer> spConnectionContainer;
      HRESULT hr = m_spWMPPlayer->
                   QueryInterface( IID_IConnectionPointContainer,
                                  (void**)&spConnectionContainer );
      if (SUCCEEDED(hr))
      {
         hr = spConnectionContainer->
              FindConnectionPoint( __uuidof(_IWMPEvents),
                                  &m_spConnectionPoint );
      }
      if (SUCCEEDED(hr))
      {
         hr = m_spConnectionPoint->Advise((IDispatch*)&m_WMPEvents,
                                          &m_dwAdviseCookie );
      }
      else
      {
         AfxMessageBox(L"Failed to get WMP control events");
         ::PostQuitMessage(0);
         return 0;
      }
      if ( FAILED(SetupWMP()) )
      {
         AfxMessageBox(L"Failed to setup WMP control");
         ::PostQuitMessage(0);
         return 0;
      }
   }
   m_spWMPPlayer->Stop();
   return TRUE;    // return TRUE  unless you set the focus to a
                   // control
}

Conclusion

With WMP control handy, your application gets additional strength in an interaction with the end user. Such multimedia support may create new added value for any product. Windows CE 5.0 brings you long-awaited support for DirectShow API to render video, but this is a good topic for future articles.

Download

Download the accompanying code's zip file here

References

Microsoft Windows Media Player 10 SDK

Microsoft Windows Media Player 8.x for Pocket PC

Mobile WMP 10 samples

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.

 

 


Sitemap | Contact Us

Rocket Fuel