用Visual C++开发的面向对象的多媒体应用软件可编译成
真正的EXE可执行文件,无需附加动态库和控件,如VBX和OCX
等。有两种方法可以实现这个功能,一种方法是使用底层AVI
文件函数,从AVI视频文件中读取视频流;另一种是使用现有的
Video forin dows SDK的窗口类MCIWnd(媒体控制界面窗)。
本文将介绍基于VC++5.0环境下,开发播放AVI文件的两种方
法,这两种方法略作改动, 还可为应用程序播放其它的媒体文
件(如声音文件等)。
一. VC++5.0可通过调用VFW.H和VFW32.LIB和MCIWnd API
函数来创建MCIWnd窗口实现AVI多媒体文件的调用和播放。
1. MCIWnd是一个控制多媒体设备(如MIDI、数字视频、VCR
以及CD音频设备等)的窗口类,要制作多媒体封面只需创建该
类的一个窗口,然后向它发送打开和控制MCI设置的消息。
MCIWnd窗口底部的播放条(Playbar )包括一个播放/暂停
(Play/Pause)按键、一个显示菜单(menu)按键和一个用于报告
播放进程的标尺。在播放窗口的任何地方单击鼠标右键都会显
示一个弹出菜单, 用于调整MCIWnd窗口的大小、MCI多媒体文
件的声音、速度的大小、MCI命令等。而且在父窗口外单击鼠
标右键可关闭和清除MCIWnd窗口,单击鼠标左键则可恢复
MCIWnd窗口。
2.编程步骤
首先在VC++5.0中建立一个名为playavi项目( project)。
然后,通过调用VC++5.0的类库,从而建立OnLButtonDown()和
OnRButtonDown()函数。在相应的视文件(playaviView.cpp)
开始处加入一行#include "vfw.h",在显示类的构造函数中加
入一行" m_mmWnd=NULL",在相应的视文件的头文件中加入:
private:
HWND m_mmWnd;
BOLL m_pause;
在源文件playavi.cpp相应的函数中输入如下代码:
// CPlayaviView message handlers
void CPlayaviView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//加入我的代码
CString filename("/filename.avi");//键入文件路径及文件名
if(m_mmWnd==NULL)
m_mmWnd=MCIWndCreate(this->GetSafeHwnd(),AfxGetInstanceHandle(),
WS_CHILD|WS_CAPTION|WS_VISIBLE|MCIWNDF_SHOWPOS|MCIWNDF_SHOWNAME,
//以上为开关选项
filename);// filename为要播放的avi文件名
else
{
//设置播放句柄
if(m_pause)
{
MCIWndPlay(m_mmWnd);
m_pause=FALSE;
}
//设置中断句柄
else
{
MCIWndPause(m_mmWnd);
m_pause=TRUE;
}
}
//结束我的代码
CView::OnLButtonDown(nFlags, point);
}
void CPlayaviView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//加入我的代码
if (m_mmWnd!=NULL)
{
MCIWndDestroy(m_mmWnd);
m_mmWnd=NULL;
}
//结束我的代码
CView::OnRButtonDown(nFlags, point);
}
最后,在Project Setting对话框的Link表中将VFW32·LIB加
入Object/Library Modules对话框,最后编译该项目,即可播放了。
二. 在VC++5.0创建MCIWnd一个窗口,然后向它发送打开和控制MCI
设置的消息,也可实现播放AVI文件。
1.编辑Stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#if !defined(AFX_STDAFX_H__A851EC08_00FE_11D2_B7EF_00C0DF81AC80__INCLUDED_)
#define AFX_STDAFX_H__A851EC08_00FE_11D2_B7EF_00C0DF81AC80__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows
headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <vfw.h>
#pragma comment(lib,"vfw32.lib")
#include <afxdisp.h> // MFC OLE automation classes
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>
// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations
immediately
//before the previous line.
#endif
// !defined(AFX_STDAFX_H__A851EC08_00FE_11D2_B7EF_00C0DF81AC80__INCLUDED_)
2.编辑CExampleApp::InitInstance()
注册MCIWnd窗口类,打开MCIWnd窗口,并播放AVI文件,最后关闭
MCIWnd窗口,然后开始应用程序的常规初始化。
BOOL CAApp::InitInstance()
{
// CG: The following block was added by the Splash Screen
component.
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
}
// CG: The following block was added by the Splash Screen
component.
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
//加入我的代码
if(!MCIWndRegisterClass())//注册MCIWnd窗口类
return FALSE;
HWND m_hAVI;//定义一个播放AVI文件的窗口句柄
m_hAVI=MCIWndCreate(NULL,AfxGetInstanceHandle(),
WS_VISIBLE|WS_POPUP,NULL);//创建MCIWnd窗口
if(m_hAVI==NULL)
return FALSE;
CString filename="/filename.avi");//键入文件路径及文件名
if(filename.GetLength()>0)
{
MCIWndOpen(m_hAVI,(LPCSTR)filename,0);//打开设备文件
if(MCIWndUseTime(m_hAVI)!=0)
//设置MCI时间格式
return FALSE;
long PlayTime=MCIWndGetLength(m_hAVI);
//获得AVI文件的播放时间长度
MCIWndPlay(m_hAVI);//播放AVI影视文件
::Sleep(PlayTime);//进程休眠
MCIWndDestroy(m_hAVI);//关闭MCI窗口
}
//结束我的代码
//开始常规初始化
#ifdef _AFXDLL
Enable3dControls();
// Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();
// Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings();
// Load standard INI file options (including MRU)
// Register the application`s document templates. Document
templates
// serve as the connection between documents, frame windows and
//views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CADoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CAView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
//CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update
//it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
三. 结束语
以上两种方法,笔者在VC++5.0中已调试通过。只要略作改动, 它
们还可为应用程序播放其它的多媒体文件(如声音文件等)。亲爱的读
者,您不妨一试!
用VC++5.0播放AVI文件的两种方法
最新推荐文章于 2019-05-28 10:51:11 发布