利用vc提供的MCIWnd类播放声音

制作VC++应用程序的多媒体封面

 

  当我们使用Visual C++(以下简称VC)开发的应用程序时,若能为应用程序制作一个生动的多媒体封面(如播放一段AVI影视)一定能使应用程序增色不少。有两种方法可以实现这个功能,一种方法是使用底层AVI文件函数,从AVI视频文件中读取视频流,尽管这种方法可以控制整个播放过程,但需要编写大量代码;另一种更简便的实现方法是使用现有的Video for W indows SDK的窗口类MCIWnd(媒体控制界面窗口),这种方法比较易于操作,现介绍如下。

  设计思想及关键技术

  MCIWnd是一个控制多媒体设备(如MIDI、数字视频、VCR以及CD音频设备等)的窗口类,要制作多媒体封面只需创建该类的一个窗口,然后向它发送打开和控制MCI设置的消息。为实现封面效果,我们应当在Initial Instance()函数执行初始化任务之前,对AVI文件进行播放,主要使用Video for Windows SDK的以下几个函数:

  1.MCIWndRegisterClass()注册MCIWnd窗口类。

  2.MCIWndCreate()

  ·函数原型

  HWND MCIWndCreate(HWND hwndParent,HINSTANCE hInstance, DWORD dwStyle,LPSTRs zFile);

  ·实现功能

  该函数创建一个使用MCI设备的窗口,返回MCI设备窗口的句柄。

  ·参数说明

  hwndParent:父窗口句柄,在本应用中父窗口应为NULL;

  hInstance: 当前实例句柄,可以用AfxGet InstanceHandle()函数获得;

  dwStyle: MCIWnd窗口的风格;

  szFile: 打开的MCI设备的名称,在此处设为NULL。

  3.MCIWndOpenO

  ·函数原型

  LONG MCIWndOpen(HWND hwnd,LPVOID szFile,UINT wFlags)

  ·实现功能

  向MCIWnd窗口发送MCIWNDM_OPEN消息,打开某MCI设备,将其关联到一个MCIWnd窗口。若调用成功则返回值为0。

  ·参数说明

  hwnd:MCI窗口句柄;

  szFile:MCI文件名;

  wFlags:设备打开方式标识。

  4.MCIWndPlay()

  ·函数原型

  LONG MCIWndPlay(HWND hwnd)

  ·实现功能

  发送MCI_PLAY消息,MCIWnd窗口接收到该消息,播放已经打开的MCI文件。

  ·参数说明

  hwnd:MCI窗口句柄;

  5.MCIWndUseTime()

  该函数将MCI设备的时间格式设置为毫秒;设置其它时间格式可以用函数MCIWndSetTime Format()实现。

  6.MCIWndGetLength()

  该函数向MCIWnd窗口发送MCIWNDM_GETLENGTH消息,根据MCI设备所使用的时间格式返回文件的长度。

  7.MCIWndDestroy()

  该函数向窗口发送一个WM_CLOSE消息,窗口接收到该消息之后,关闭所打开的MCI文件,并关闭窗口。虽然SDK还提供了一个MCIWndClose函数,但该函数只能关闭在MCIWnd窗口中打开的文件,而MCIWnd窗口仍处于开启状态,仍可以打开其它MCI文件并进行播放。

 

 

 实现步骤

  假设我们已经通过MFC AppWizard(EXE)建立了一个名为Example的工程,则该应用至少包含有CExampleApp、CMainFrame、CExampleView三个类。

  下面我们给这个应用加入多媒体封面,具体实现步骤如下:

  1.编辑Stdafx.h

  在Stdafx.h中放入包含文件可以使用预编译头文件中的所有多媒体信息。由于项目中的每一个文件已经包括了Stdafx.h,所以在其它地方不必包含这些多媒体文件。在Stdafx.h中放入包含文件可以使用预编译头文件中的所有多媒体信息。由于项目中的每一个文件已经包括了Stdafx.h,所以在其它地方不必包含这些多媒体文件。

#include<afxwin.h> //MFC core and standard

components

#include<afxext.h> //MFC extensions

#include<vfw.h>

#pragma comment(lib,"vfw32.lib")

  2.编辑CExampleApp::InitInstance()

  注册MCIWnd窗口类,打开MCIWnd窗口,并播放AVI文件,最后关闭MCIWnd窗口,然后开始应用程序的常规初始化。

BOOL CExampleApp::InitInstance()

{

if(!MCIWndRegisterClass()) //注册MCIWnd窗口类

return FALSE;

HWNDm_hAVI //定义一个播放AVI文件的窗口句柄

m_hAVI=MCIWndCreate(NULL,AfxGetInstanceHandle(),

MCIWNDF_NOPLAYBAR|

WS_VISIBLE|

WS_POPUT,

NULL); //创建MCIWnd窗口

if(m_hAVI=NULL)

return FALSE;

constCString filename="d:\\zhp\\example.avi" //AVI文件名

if(filename.GetLength()>0)

{

MCIWndOpen(m_hAVI,(LPCSTR)filename,0); //打开设备文件

if(MCIWndUseTime(m_haAVI)!=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

……

}

  由于Windows系统是多任务、多进程并行的,因此要让InitialInstance进程休眠,等待M CIWndPlay进程播放AVI文件结束后才能继续执行InitialInstance进程,否则系统将在播放A VI影视的同时执行应用程序的初始化,不能达到动画封面效果。在以上代码中,阴影部分的程序段的功能就是获得AVI文件播放时间长度PlayTime,然后使用::Sleep()函数让进程休眠Pl ayTime毫秒后关闭MCIWnd窗口。

 

 

 

------------------------------------------------------------------------------------------------

 

使用MCIWnd,源文件中需要包含头文件 vfw.h,在Project->Settings->Link->Object/libray module中加入库 vfw32.lib。

1、MCIWnd的创建

MCIWnd子窗口的创建可使用MCIWndCreate函数:
HWND MCIWndCreate(
HWND hwndParent,   //父窗口句柄
HINSTANCE hInstance, //应用程序的实例句柄
DWORD dwStyle,    //显示风格
LPSTR szFile     //多媒体文件名
);
返回的HWND可以保存下来,以供以后使用,也可不保存。
该函数会在其父窗口上创建一个子窗口,类似于创建一个控间如按扭或列表框等。
该子窗口会占据父窗口一定空间,可带有播放按钮、进度条、菜单按钮等。

图3.3.1 MCIWnd子窗口
示例:
(1)建一个多文档的MFC应用程序。
(2)在View类的头文件中加入变量:
HWND m_mciWnd;
(3)在View类中用ClassWizard重载OnInitialUpdate函数。
(4)在此函数中加入代码:
m_mciWnd=MCIWndCreate(m_hWnd, AfxGetInstanceHandle(), MCIWNDF_SHOWALL | MCIWNDF_RECORD, GetDocument()->GetPathName());
这里,m_hWnd为此View窗口的HWND,
AfxGetInstanceHandle()可取得本应用程序的实例句柄,
MCIWNDF_flag们决定了子窗口中是否要加入播放按钮、录音按钮、菜单按钮、进度条等控件,
GetDocument()->GetPathName()则可获得通过打开文件对话框取得的文件名。
(5)编译运行。
(6)在运行的程序中已经可以任意打开一个多媒体文件进行播放。例如 .wav、.avi、.mid文件。

2、MCIWnd的使用

如果在MCIWnd子窗口中有播放按钮、录音按钮、菜单按钮、进度条等控件,可以通过它们操作多媒体。
如果象上例一样保存了MCIWndCreate函数返回的HWND,则不管子窗口中是否有控件,都可通过MCIWndxxxx函数操作多媒体。

(7)在上面的程序中加入ID为 ID_NEW、ID_OPEN、ID_PLAY、ID_PLAYREVERSE、ID_RECORD、ID_SAVE、ID_STOP、ID_CLOSE 的菜单项或Toolbar按钮。
(8)在 ID_NEW 的消息响应函数中加入:
MCIWndNew(m_mciWnd,"waveaudio");
MCIWnd子窗口可以建立一个新的音频多媒体文件。
(9)在 ID_OPEN 的消息响应函数中加入:
MCIWndOpen(m_mciWnd,"c:\\MyWav.wav",0);
MCIWnd子窗口可以打开一个已存在的多媒体文件。
(10)在 ID_PLAY 的消息响应函数中加入:
MCIWndPlay(m_mciWnd);
MCIWnd子窗口可以播放多媒体文件。
(11)在 ID_PLAYREVERSE 的消息响应函数中加入:
MCIWndPlayReverse(m_mciWnd);
MCIWnd子窗口可以倒着播放视频多媒体文件。
(12)在 ID_RECORD 的消息响应函数中加入:
MCIWndRecord(m_mciWnd);
MCIWnd子窗口可以录制音频多媒体文件。
(13)在 ID_SAVE 的消息响应函数中加入:
MCIWndSave(m_mciWnd,"c:\\MyWav.wav");
MCIWnd子窗口可以保存已录制的音频多媒体文件。
(14)在 ID_STOP 的消息响应函数中加入:
MCIWndStop(m_mciWnd);
MCIWnd子窗口可以停止正在播放或录制的多媒体文件。
(15)在 ID_CLOSE 的消息响应函数中加入:
MCIWndClose(m_mciWnd);
MCIWnd子窗口可以关闭当前的多媒体文件,若要再使用,必须重新打开。
(16)编译运行。

 

-----------------------------------------------------------------------

 

MCIWndCreate

The MCIWndCreate function registers the MCIWnd window class and creates an MCIWnd window for using MCI services.MCIWndCreate can also open an MCI device or file (such as an AVI file) and associate it with the MCIWnd window.


  
  
  1. HWND MCIWndCreate(
  2.   HWND hwndParent,      
  3.   HINSTANCE hInstance,  
  4.   DWORD dwStyle,        
  5.   LPCTSTR szFile          
  6. );
  7.  

Parameters

hwndParent

Handle to the parent window.

hInstance

Handle to the module instance to associate with the MCIWnd window.

dwStyle

Flags defining the window style. In addition to specifying the window styles used with theCreateWindowEx function, you can specify the following styles to use with MCIWnd windows.

ValueMeaning
MCIWNDF_NOAUTOSIZEWINDOWWill not change the dimensions of an MCIWnd window when the image size changes.
MCIWNDF_NOAUTOSIZEMOVIEWill not change the dimensions of the destination rectangle when an MCIWnd window size changes.
MCIWNDF_NOERRORDLGInhibits display of MCI errors to users.
MCIWNDF_NOMENUHides the Menu button from view on the toolbar and prohibits users from accessing its pop-up menu.
MCIWNDF_NOOPENHides the open and close commands from the MCIWnd menu and prohibits users from accessing these choices in the pop-up menu.
MCIWNDF_NOPLAYBARHides the toolbar from view and prohibits users from accessing it.
MCIWNDF_NOTIFYANSICauses MCIWnd to use an ANSI string instead of a Unicode string when notifying the parent window of device mode changes. This flag is used in combination with MCIWNDF_NOTIFYMODE and is exclusive to Windows NT/Windows 2000.
MCIWNDF_NOTIFYMODECauses MCIWnd to notify the parent window with an MCIWNDM_NOTIFYMODE message whenever the device changes operating modes. ThelParam parameter of this message identifies the new mode, such as MCI_MODE_STOP.
MCIWNDF_NOTIFYPOSCauses MCIWnd to notify the parent window with an MCIWNDM_NOTIFYPOS message whenever a change in the playback or record position within the content occurs. ThelParam parameter of this message contains the new position in the content.
MCIWNDF_NOTIFYMEDIACauses MCIWnd to notify the parent window with an MCIWNDM_NOTIFYMEDIA message whenever a new device is used or a data file is opened or closed. ThelParam parameter of this message contains a pointer to the new file name.
MCIWNDF_NOTIFYSIZECauses MCIWnd to notify the parent window when the MCIWnd window size changes.
MCIWNDF_NOTIFYERRORCauses MCIWnd to notify the parent window when an MCI error occurs.
MCIWNDF_NOTIFYALLCauses all MCIWNDF window notification styles to be used.
MCIWNDF_RECORDAdds a Record button to the toolbar and adds a new file command to the menu if the MCI device has recording capability.
MCIWNDF_SHOWALLCauses all MCIWNDF_SHOW styles to be used.
MCIWNDF_SHOWMODEDisplays the current mode of the MCI device in the window title bar. For a list of device modes, see theMCIWndGetMode macro.
MCIWNDF_SHOWNAMEDisplays the name of the open MCI device or data file in the MCIWnd window title bar.
MCIWNDF_SHOWPOSDisplays the current position within the content of the MCI device in the window title bar.

szFile

Null-terminated string indicating the name of an MCI device or data file to open.

Return Values

Returns the handle to an MCI window if successful or zero otherwise.

Remarks

Default window styles for a child window are WS_CHILD, WS_BORDER, and WS_VISIBLE.MCIWndCreate assumes a child window when a non-NULL handle of a parent window is specified.

Default window styles for a parent window are WS_OVERLAPPEDWINDOW and WS_VISIBLE.MCIWndCreate assumes a parent window when a NULL handle of a parent window is specified.

Use the window handle returned by this function for the window handle in the MCIWnd macros. If your application uses this function, it does not need to use theMCIWndRegisterClass function.

Requirements

  Windows NT/2000/XP: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Vfw.h.
  Library: Use Vfw32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP.

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值