
vc200320052008调用mediaplayer的指南.docx
16页VC++200320052008调用Mediaplayer的指南,通过对CWMPPlayer4、CWMPSettings和CWMPControls等几个类的使用可以实现WindowsMediaPlayer的大部分常规功能,如果需要做进一步的控制,可以在用下面这些函数返回相关类对象后调用相关成员函数来加以实现:GetCurrentMedia()返回CWMPMedia类对象GetMediaCollection()返回CWMPMediaCollection类对象GetPlaylistCollection()返回CWMPPlaylistCollection类对象GetNetwork()返回CWMPNetwork类对象GetCurrentPlaylist()返回CWMPPlaylist类对象GetCdromCollection()返回CWMPCdromCollection类对象GetClosedCaption()返回CWMPClosedCaption类对象GetError()返回CWMPError类对象GetDvd()返回CWMPDVD类对象GetPlayerApplication()返回CWMPPlayerApplication类对象1、新建一个基于对话框的mfc应用程序。
2、在工具箱上点右键选择“选择项”,打开com组件,选择windowsmediaplayer添加到工具箱上3、 把media控件拖到窗体上.选中窗体在菜单上选择项目-〉添加类-〉MFC->activex控件中的mfc类-〉添加4、 选择windowsmediaplayer<1.0>,添加IWMPControls类5、在xxxDlg.h中添加:#include"CWMPControls.h"public:COcx1mm;//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;RECTrcClient;CComPtr<IObjectWithSite>spHostObject;CComPtr<IAxWinHostWindow>spHost;*pRemoteHost =CComObject<CRemoteHost>NULL;//创建ActiveX控件容器AtlAxWinInit();m_pView=newCAxWindow();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)){m_pView->QueryHost(IID_IObjectWithSite,(void**)&spHostObject);if(!spHostObject.p){hr=E_POINTER;}}}//创建IServiceProviderandIWMPRemoteMediaServicesif(SUCCEEDED(hr)){hr=CComObject<CRemoteHost>::CreateInstance(&pRemoteHost);if(pRemoteHost){pRemoteHost->AddRef();}else{hr=E_POINTER;}//Setsitetotheremotehostif(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(L"{6BF52A52-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;}}//Setskintobecustomskinif(SUCCEEDED(hr)){DispEventAdvise(m_spPlayer);//Hooktheeventlistenerhr=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控件”—〉选择windowsmediaplayer—〉将控件拉到适合大小—〉右击player控件—〉选择添加变量(m_avPlayer)。
此时添加的变量在VS2003中不在时IWMPPlay4了,而是COcx右击类视图/解决方案—〉添加类—〉选择“Active中的MFC类”一〉选择windowsmediaplayer<1.0>&选择相应的接口(IWMPMediaIWMPSettingsIWMPControlsIWMPPlaylist等等)生成类为图方便,我将所有接口都生成了相应的类为了说明方便,我在对话框中放了几个按钮,分别是:打开,播放,暂停,停止,添加并在对话框右边添加了一个clistctrl又来显示播放列表然后在对话框头文件中添加了以下变量:CWMPControlsm_control;CWMPSettingsm_set;CWMPPlaylistm_playlist;CWMPMediam_media;COcx1m_avPlayer;CListCtrlm_listCtrl;mediaplayer的主要功能有:播放控制,屏幕显示,播放列表,播放列表集,播放信息显示l其中播放控制又有:打开,开始,暂停,停止,快速播放,倒退等等1. 打开:当选择一个文件打开是,可以直接使用m_avPlayer.put_URL(fileName);为打开按钮添加消息响应函数,在函数中添加如下代码:charszFileFilter[]="Mp3File(*.mp3)|*.mp3|""MPEGFile(*.mpeg)|*.mpeg|""MediaFile(*.asf)|*.asf|""VideoFile(*.dat)|*.dat|""MPGAFile(*.mpga)|*.mpga|""WaveFile(*.wav)|*.wav|""AVIFile(*.avi)|*.avi|""MovieFile(*.mov)|*.mov|""MidFile(*.mid;*,rmi)|*.mid;*.rmi|""WmaFile(*.wma)|*.wma|""AllFile(*.*)|*.*||";CFileDialogfileDialog(TRUE,NULL,NULL,NULL,szFileFilter,this);if(fileDialog.DoModal()==IDOK){CStringfileName=fileDialog.GetPathName();m_avPlayer.put_URL(fileName);}这样当选择一个播放文件后,mediaplayer就会自动播放该文件了。
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());然后将打开按钮的消息响应函数改为:charszFileFilter[]="Mp3File(*.mp3)|*.mp3|""MPEGFile(*.mpeg)|*.mpeg|""MediaFile(*.asf)|*.asf|""VideoFile(*.dat)|*.dat|""MPGAFile(*.mpga)|*.mpga|""WaveFile(*.wav)|*.wav|""AVIFile(*.avi)|*.avi|""MovieFile(*.mov)|*.mov|""MidFile(*.mid;*,rmi)|*.mid;*.rmi|""WmaFile(*.wma)|*.wma|"AllFile(*.*)|*.*||";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。












