2023年12月31日发(作者:)

WinCE中基于Media Player的多媒体开发

在过去的10年里,移动技术已经取得了令人难以置信的进步和成就。应用Windows CE系统的移动设备也在应用和编程方面变得更加灵活和方便。最新的Windows CE设备中加入了Windows Media Player 10 Mobile,它提供了和PC上的WMP控件一样强大的功能。你可以为你的移动设备增加多媒体能力,包括播放视频、音频文件,展示图片等等。你可以在文后所附的段落中找到可用的SDK和资源等相关信息。

下面我们简单介绍WMP的一些实现技术。

慨述

WMP SDK提供了很多的接口,但不是所有都能应用到Windows

Mobile平台上来。下面列出可用的一些并进行解释:

接口

IWMPCore

描述

WMP对象模型的根接口。你可以由此获取其他接口的指针并且通过它访问其他空间的基本特性。

允许一个应用程序访问Windows Media Player控件;如它的播放,停止和暂停按钮。

提供错误信息。

把由Windows Media Player控件产生的事件提供给一个可以反馈的嵌入式程序。

管理媒体项的属性。

IWMPControls

IWMPError

IWMPEvents

IWMPMedia,IWMPMediaCollection

IWMPNetwork

IWMPPlayer

IWMPPlaylist,

IWMPPlaylistArray,

IWMPPlaylistCollection

IWMPSettings

设置和获取Windows Media Player所使用的网络连接

控制Windows Media Player空间的用户接口的行为。

播放列表操作。

设置或者获取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_(m_hWnd, rcClient, NULL, WS_CHILD | WS_VISIBLE |

WS_CLIPCHILDREN | WS_CLIPSIBLINGS);

if (NULL == m_wndView.m_hWnd) goto FAILURE;

// 在窗口中装载OCX

hr = m_ost(&spHost);

if (FAILMSG(hr)) goto FAILURE;

hr = spHost->CreateControl(CComBSTR(_T("")),

m_wndView, 0);

if (FAILMSG(hr)) goto FAILURE;

hr = m_ontrol(&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 (l(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 ", ImgVideoPlay,

true)'>

<IMG id="ImgVideoPlay" src="bt_" border="0">

</a>

Video<br>

<a href=# OnClick='PlayClip("storage ", ImgAudioPlay,

false)'>

<IMG id="ImgAudioPlay" src="bt_" 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()

{

();

();

if (CurrentPlayImage != null)

{

= "bt_";

}

bWasBufferring = false;

}

function ClipPlayState(NewState)

{

= + NewState + " ";

switch(NewState) {

case 1:

// stopped

if (bWasBuffering)

{

bWasBufferring = false;

if (CurrentPlayImage != null)

{

= "bt_";

}

}

break;

case 6:

// buffering

bWasBufferring = true;

if (CurrentPlayImage != null)

{

= "bt_";

}

break;

case 9:

// transitioning

case 11:

// reconnecting

bWasBufferring = false;

break;

case 3:

// playing

if (bWasBufferring)

{

if (CurrentPlayImage != null)

{

= "bt_";

}

if (bVideo)

{

reen = true;

}

}

break;

default:

}

}

function PlayClip(url, img, video)

{

if (ate == 3 && bVideo != null && bVideo != video)

{

return;

}

bVideo = video;

CurrentPlayImage = img;

if (ate == 3)

{

StopPlayer();

}

else

{

= "";

if (CurrentPlayImage != null)

{

= "bt_";

}

= 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_entRect(&rect);

if ( m_Control(__uuidof(WMP),L"",

WS_VISIBLE|WS_CHILD,rect, &m_Panel,AFX_IDW_PANE_FIRST) )

{

LPUNKNOWN lpUnk = m_trolUnknown();

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

}