WinCE中基於Media Player的多媒體開發

WinCE中基於Media Player的多媒體開發
 

發佈日期: 2005-12-8 20:33:56 作者:  字體:【 】【打印本頁

  在過去的10年裏,移動技術已經取得了令人難以置信的進步和成就。應用Windows CE系統的移動設備也在應用和編程方面變得更加靈活和方便。最新的Windows CE設備中加入了Windows Media Player 10 Mobile,它提供了和PC上的WMP控件一樣強大的功能。你可以爲你的移動設備增加多媒體能力,包括播放視頻、音頻文件,展示圖片等等。你可以在文後所附的段落中找到可用的SDK和資源等相關信息。
下面我們簡單介紹WMP的一些實現技術。

  慨述

  WMP SDK提供了很多的接口,但不是所有都能應用到Windows Mobile平臺上來。下面列出可用的一些並進行解釋:

接口 描述
IWMPCore WMP對象模型的根接口。你可以由此獲取其他接口的指針並且通過它訪問其他空間的基本特性。
IWMPControls 允許一個應用程序訪問Windows Media Player控件;如它的播放,停止和暫停按鈕。
IWMPError 提供錯誤信息。
IWMPEvents 把由Windows Media Player控件產生的事件提供給一個可以反饋的嵌入式程序。
IWMPMedia,IWMPMediaCollection 管理媒體項的屬性。
IWMPNetwork 設置和獲取Windows Media Player所使用的網絡連接
IWMPPlayer 控制Windows Media Player空間的用戶接口的行爲。

IWMPPlaylist,
IWMPPlaylistArray,
IWMPPlaylistCollection

播放列表操作。
IWMPSettings 設置或者獲取Windows Media Player的設置。

  創建第一個應用程序

  下面由一個簡單的ATL應用程序開始,你將創建一個空間容器窗口。下面的代碼段使用了一個標準的ATL技術來展示Windows Media Player控件:

LRESULT CWMPHost::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
 AtlAxWinInit();
 CComPtr<IAxWinHostWindow> spHost;
 CComPtr<IConnectionPointContainer> spConnectionContainer;
 CComWMPEventDispatch *pEventListener = NULL;
 CComPtr<IWMPEvents> spEventListener;
 HRESULT hr;
 RECT rcClient;
 m_dwAdviseCookie = 0;
 ...
 // 創建窗口
 GetClientRect(&rcClient);
 m_wndView.Create(m_hWnd, rcClient, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
 if (NULL == m_wndView.m_hWnd) goto FAILURE;
 // 在窗口中裝載OCX
 hr = m_wndView.QueryHost(&spHost);
 if (FAILMSG(hr)) goto FAILURE;
 hr = spHost->CreateControl(CComBSTR(_T("WMPlayer.OCX")), m_wndView, 0);
 if (FAILMSG(hr)) goto FAILURE;
 hr = m_wndView.QueryControl(&m_spWMPPlayer);
 if (FAILMSG(hr)) goto FAILURE;
 // 開始監聽事件
 hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
 spEventListener = pEventListener;
 if (FAILMSG(hr)) goto FAILURE;
 hr = m_spWMPPlayer->QueryInterface(__uuidof(IConnectionPointContainer), (void**)&spConnectionContainer); if (FAILMSG(hr)) goto FAILURE;
 // 看OCX 是否支持IWMPEvents接口
 hr = spConnectionContainer->FindConnectionPoint(__uuidof(IWMPEvents), &m_spConnectionPoint);
 if (FAILMSG(hr)) goto FAILURE;
 hr = m_spConnectionPoint->Advise(spEventListener, &m_dwAdviseCookie);
 if (FAILMSG(hr)) goto FAILURE;
 return 0;FAILURE: ::PostQuitMessage(0);
 return 0;
}

  你所要做的只是創建一個控件窗口,包含一個IWMPPlayer接口指針,並且對WMP事件進行響應。ATL可以用比MFC更加簡便的方式來完成這些任務,當然你也可以使用MFC。你的程序現在就可以播放Windows媒體文件,比如WMA和WMV。


WMP

  WMP控件也允許程序員來控制它的行爲,比如你可以這樣開始播放一個媒體文件:

LRESULT CWMPHost::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
 CFileOpenDlg dlgOpen;
 HRESULT hr;
 if (dlgOpen.DoModal(m_hWnd) == IDOK)
 {
  hr = m_spWMPPlayer->put_URL(dlgOpen.m_bstrName);
  if (FAILMSG(hr))
   return 0;
 }
 return 0;
}


  Windows Mobile Player 10的移動範例提供了一系列的控件使用範例。在Web應用程序裏使用WMP OCX

  在使用Web瀏覽器的時候,應用WMP編程就更加容易了(這是SDK裏的例子):

<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>

 

    使用舊版本的WMP控件

  如果你使用的手持設備沒有WMP10,那事情還沒有完。你仍然還可以使用WMP OCX版本8來爲你的Pocket PC編程,雖然提供的功能特性少,好在也可以基本滿足需要。我創建了一個簡單的工程來展示它如何在MFC環境下工作。下面的代碼段證明了它和ATL方式一樣簡單:

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
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章