vc调用mediaplayer的指南.docx
- 文档编号:3518763
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:8
- 大小:17.41KB
vc调用mediaplayer的指南.docx
《vc调用mediaplayer的指南.docx》由会员分享,可在线阅读,更多相关《vc调用mediaplayer的指南.docx(8页珍藏版)》请在冰豆网上搜索。
vc调用mediaplayer的指南
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;
CComObject<CRemoteHost>*pRemoteHost=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))
{
hr=m_pView->QueryHost(IID_IObjectWithSite,(void**)&spHostObject);
if(!
spHostObject.p)
{
hr=E_POINTER;
}
}
}
//创建IServiceProviderandIWMPRemoteMediaServices
if(SUCCEEDED(hr))
{
hr=CComObject<CRemoteHost>:
:
CreateInstance(&pRemoteHost);
if(pRemoteHost)
{
pRemoteHost->AddRef();
}
else
{
hr=E_POINTER;
}
}
//Setsitetotheremotehost
if(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;
}
}
//Setskintobecustomskin
if(SUCCEEDED(hr))
{
DispEventAdvise(m_spPlayer);//Hooktheeventlistener
hr=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_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();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vc 调用mediaplayer的指南 调用 mediaplayer 指南