vc200320052008 调用mediaplayer的指南.pdf
VC+200320052008 调用 Mediaplayer 的指南,通过对CWMPPlayer4、CWMPSettings 和CWMPControls等几个类的使用可以实现 Windows Media Player 的大部分常规功能,如果需要做进一步的控制,可以在用下面这些函数返回相关类对象后调用相关成员函数来加以实现:GetCurrentMedia()返回 CWMPMedia 类对象GetMediaCollection()返回 CWMPMediaCollection 类对象GetPlaylistCollection()返回 CWMPPlaylistCollection 类对象GetNetwork()返回 CWMPNetwork 类对象GetCurrentPlaylist()返回 CWMPPlaylist 类对象GetCdromCollection()返回 CWMPCdromCollection 类对象GetClosedCaption()返回 CWMPClosedCaption 类对象GetError()返回 CWMPError 类对象GetDvd()返回CWMPDVD 类对象GetPlayerApplication()返回CWMPPlayerApplication 类对象1、新建一个基于对话框的 mfc 应用程序。2、在工具箱上点右键选择“选择项”,打开com 组件,选择windows media player 添加到工具箱上。3、把 media 控件拖到窗体上.选中窗体。在菜单上选择 项目-添加类-MFC->activex 控件中的 mfc 类-添加。4、选择 windows media player<1.0>,添加 IWMPControls类。5、在 xxxDlg.h 中添加:#include CWMPControls.h。public:COcx1 mm;/Ocx1 为 media 控件,mm 为控件变量。CWMPControlsm_control;6、在 xxxDlg.cpp 文件中 OnInitDialog()函数中添加:m_control=static_cast<CWMPControls>(mm.get_controls();7、现在你可以用 mm.put_URL 去干你的事情了。另外一种办法,直接用 COM 操作,例如:CAxWindow*m_pView;CComObject<IWMPPlayer4>m_spPlayer;RECT rcClient;CComPtr<IObjectWithSite>spHostObject;CComPtr<IAxWinHostWindow>spHost;CComObject<CRemoteHost>*pRemoteHost=NULL;/创建 ActiveX 控件容器AtlAxWinInit();m_pView=new CAxWindow();if(!m_pView)hr=E_OUTOFMEMORY;if(SUCCEEDED(hr):GetWindowRect(GetDlgItem(IDC_RANGE),&rcClient);ScreenToClient(&rcClient);m_pView->Create(m_hWnd,rcClient,NULL,WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,WS_EX_CLIENTEDGE);if(:IsWindow(m_pView->m_hWnd)hr=m_pView->QueryHost(IID_IObjectWithSite,(void*)&spHostObject);if(!spHostObject.p)hr=E_POINTER;/创建 IServiceProvider and IWMPRemoteMediaServicesif(SUCCEEDED(hr)hr=CComObject<CRemoteHost>:CreateInstance(&pRemoteHost);if(pRemoteHost)pRemoteHost->AddRef();elsehr=E_POINTER;/Set site to the remote hostif(SUCCEEDED(hr)hr=spHostObject->SetSite(IWMPRemoteMediaServices*)pRemoteHost);if(SUCCEEDED(hr)hr=m_pView->QueryHost(&spHost);if(!spHost.p)hr=E_NOINTERFACE;/创建 WindowsMediaPlayer 的控件if(SUCCEEDED(hr)hr=spHost->CreateControl(CComBSTR(L6BF52A52-394A-11d3-B153-00C04F79FAA6),m_pView->m_hWnd,NULL);if(SUCCEEDED(hr)hr=m_pView->QueryControl(&m_spPlayer);if(!m_spPlayer.p)hr=E_NOINTERFACE;/Set skin to be custom skinif(SUCCEEDED(hr)DispEventAdvise(m_spPlayer);/Hook the eventlistenerhr=m_spPlayer->put_uiMode(CComBSTR(_T(custom);.m_spPlayer->put_URL(.);CComPtr<IWMPControls>spControls;m_spPlayer->get_controls(&spControls);if(spControls.p)spControls->play();.首先,创建一个对话框,然后在资源管理器中右击对话框,选择“插入 Active 控件”选择 windows media player将控件拉到适合大小右击 player 控件选择添加变量(m_avPlayer)。此时添加的变量在 VS2003 中不在时IWMPPlay4 了,而是 COcx。右击类视图/解决方案添加类选择“Active 中的 MFC类”选择 windows media player<1.0>选择相应的接口(IWMPMedia IWMPSettings IWMPControlsIWMPPlaylist 等等)生成类。为图方便,我将所有接口都生成了相应的类。为了说明方便,我在对话框中放了几个按钮,分别是:打开,播放,暂停,停止,添加。并在对话框右边添加了一个 clistctrl又来显示播放列表。然后在对话框头文件中添加了以下变量:CWMPControls m_control;CWMPSettings m_set;CWMPPlaylistm_playlist;CWMPMedia m_media;COcx1 m_avPlayer;CListCtrl m_listCtrl;media player 的主要功能有:播放控制,屏幕显示,播放列表,播放列表集,播放信息显示。l其中播放控制又有:打开,开始,暂停,停止,快速播放,倒退等等。1.打开:当选择一个文件打开是,可以直接使用m_avPlayer.put_URL(fileName);为打开按钮添加消息响应函数,在函数中添加如下代码:char szFileFilter=Mp3 File(*.mp3)|*.mp3|MPEG File(*.mpeg)|*.mpeg|Media File(*.asf)|*.asf|Video File(*.dat)|*.dat|MPGA File(*.mpga)|*.mpga|Wave File(*.wav)|*.wav|AVI File(*.avi)|*.avi|Movie File(*.mov)|*.mov|Mid File(*.mid;*,rmi)|*.mid;*.rmi|Wma File(*.wma)|*.wma|All File(*.*)|*.*|;CFileDialogfileDialog(TRUE,NULL,NULL,NULL,szFileFilter,this);if(fileDialog.DoModal()=IDOK)CStringfileName=fileDialog.GetPathName();m_avPlayer.put_URL(fileName);这样当选择一个播放文件后,media player 就会自动播放该文件了。2.开始:为了实现播放控制,在 OnInitDialog()中添加代码:m_control=static_cast<CWMPControls>(m_avPlayer.get_controls();然后为开始按钮添加消息响应函数,并在函数中添加代码:m_control.play();3.暂停:m_control.pause();4.停止:m_control.stop();5.其他如倒退,快速播放,MSDN 上有相应的函数,读者可依次自行解决。到此,一个带有简单播放控制的播放器就完成。l下面来实现播放列表。为实现播放列表控制,首先在 OnInitDialog()中添加代码:/创建一个播放列表m_playlist=static_cast<CWMPPlaylist>(m_avPlayer.get_currentPlaylist();然后将打开按钮的消息响应函数改为:char szFileFilter=Mp3 File(*.mp3)|*.mp3|MPEG File(*.mpeg)|*.mpeg|Media File(*.asf)|*.asf|Video File(*.dat)|*.dat|MPGA File(*.mpga)|*.mpga|Wave File(*.wav)|*.wav|AVI File(*.avi)|*.avi|Movie File(*.mov)|*.mov|Mid File(*.mid;*,rmi)|*.mid;*.rmi|Wma File(*.wma)|*.wma|All File(*.*)|*.*|;CFileDialogfileDialog(TRUE,NULL,NULL,NULL,szFileFilter,this);if(fileDialog.DoModal()=IDOK)CStringfileName=fileDialog.GetPathName();/创建一个媒体m_media=static_cast<CWMPMedia>(m_avPlayer.newMedia(fileName);/将媒体添加到播放列表m_playlist.appendItem(m_media);/将列表添加到当前列表,以便实现自动播放m_avPlayer.put_currentPlaylist(m_playlist);m_set=(CWMPSettings)m_avPlayer.get_settings();/参数 true 设置自动播放,false 不设置自动播放m_set.put_autoStart(true);/将媒体的名字添加到列表控件m_listCtrl.InsertItem(0,m_media.get_name();l接着是屏幕显示控制m_avPlayer.put_fullScreen(true/false);l使用播放列表自动播放文件,第一步:需要在播放列表中添加媒体;第二步:将列表设置为当前播放列表;第三步:将播放设置为自动播放。函数如下:/创建媒体m_media=static_cast<CWMPMedia>(m_avPlayer.newMedia(fileName);/将媒体追加到播放列表m_playlist.appendItem(m_media);/将类别设置为当前列表m_avPlayer.put_currentPlaylist(m_playlist);/设置自动播放m_setting.put_autoStart(true);l播放信息的显示可以通过 CWMPMedia 类获取相关的媒体信息。获取播放文件名:media.get_name();