想做一个属于自己的音乐播放器吗,那么就来使用vfw.h提供的MCIWnd来实现吧。
名词解释
vfw.h - Video for windows include file for WIN32
MCIWnd - Window class for MCI objects
简要介绍
/**************************************************************************** * * MCIWnd - Window class for MCI objects * ***************************************************************************/ /*----------------------------------------------------------------------------*/ * * MCIWnd * * MCIWnd window class header file. * * the MCIWnd window class is a window class for controling MCI devices * MCI devices include, wave files, midi files, AVI Video, cd audio, * vcr, video disc, and others.. * * to learn more about MCI and mci command sets see the * "Microsoft Multimedia Programmers's guide" in the Win31 SDK * * the easiest use of the MCIWnd class is like so: * * hwnd = MCIWndCreate(hwndParent, hInstance, 0, "chimes.wav"); * ... * MCIWndPlay(hwnd); * MCIWndStop(hwnd); * MCIWndPause(hwnd); * .... * MCIWndDestroy(hwnd); * * this will create a window with a play/pause, stop and a playbar * and start the wave file playing. * * mciwnd.h defines macros for all the most common MCI commands, but * any string command can be used if needed. * * Note: unlike the mciSendString() API, no alias or file name needs * to be specifed, since the device to use is implied by the window handle. * * MCIWndSendString(hwnd, "setaudio stream to 2"); * * (C) Copyright Microsoft Corp. 1991-1995. All rights reserved. * * You have a royalty-free right to use, modify, reproduce and * distribute the Sample Files (and/or any modified version) in * any way you find useful, provided that you agree that * Microsoft has no warranty obligations or liability for any * Sample Application Files. * * If you did not get this from Microsoft Sources, then it may not be the * most current version. This sample code in particular will be updated * and include more documentation. * * Sources are: * CompuServe: WINSDK forum, MDK section. * Anonymous FTP from ftp.uu.net vendor/microsoft/multimedia * * WIN32: * * MCIWnd supports both ansi and unicode interfaces. For any message that * takes or returns a text string, two versions of the message are defined, * appended with A or W for Ansi or Wide Char. The message or api itself * is defined to be one or other of these depending on whether you have * UNICODE defined in your application. * Thus for the api MCIWndCreate, there are in fact two apis, * MCIWndCreateA and MCIWndCreateW. If you call MCIWndCreate, this will be * re-routed to MCIWndCreateA unless UNICODE is defined when building your * application. In any one application, you can mix calls to the * Ansi and Unicode entrypoints. * * If you use SendMessage instead of the macros below such as MCIWndOpen(), * you will see that the messages have changed for WIN32, to support Ansi * and Unicode entrypoints. In particular, MCI_OPEN has been replaced by * MCWNDM_OPENA, or MCIWNDM_OPENW (MCIWNDM_OPEN is defined to be one or * other of these). * * Also, note that the WIN32 implementation of MCIWnd uses UNICODE * so all apis and messages supporting ANSI strings do so by mapping them * UNICODE strings and then calling the corresponding UNICODE entrypoint. * *----------------------------------------------------------------------------*/
需要的头文件与库文件
#include <vfw.h> #pragma comment(lib,"vfw32.lib")
简单实现
要实现一个播放器,首先要先建立一个MFC应用程序,例如一个基于Dialog的MFC程序。一个简单实现的代码如下:
HWND m_hAudio; m_hAudio = MCIWndCreate( GetSafeHwnd(), AfxGetInstanceHandle(), WS_CHILD | MCIWNDF_NOMENU, _T("音乐.mp3") ); if( MCIWndCanPlay( m_hAudio ) ) { MCIWndPlay( m_hAudio ); }
这样,就可以播放出音乐来。支持的格式有wave files, midi files, AVI Video, cd audio, vcr, video disc, and others..,自己尝试了一下,好像还可以播放视频中的音轨。
常用的API函数
/** 窗口句柄所指对象是否可播放 */ MCIWndCanPlay(hwnd) /** 窗口句柄所指对象是否可录音 */ MCIWndCanRecord(hwnd) /** 进行录音 */ MCIWndRecord(hwnd) /** 关闭窗口句柄 */ MCIWndClose(hwnd) /** 播放窗口句柄所指对象 */ MCIWndPlay(hwnd) /** 停止播放窗口句柄所指对象 */ MCIWndStop(hwnd) /** 暂停播放窗口句柄所指对象 */ MCIWndPause(hwnd) /** 恢复(暂停)播放窗口句柄所指对象 */ MCIWndResume(hwnd) /** 移动播放进度至指定位置 */ MCIWndSeek(hwnd, lPos) /** 移动播放进度至初始位置 */ MCIWndHome(hwnd) /** 移动播放进度至结束位置 */ MCIWndEnd(hwnd) /** 倒退播放窗口句柄所指对象 */ MCIWndPlayReverse(hwnd) /** 从指定位置播放窗口句柄所指对象 */ MCIWndPlayFrom(hwnd, lPos) /** 播放窗口句柄所指对象直至指定位置 */ MCIWndPlayTo(hwnd, lPos) /** 播放窗口句柄所指对象的指定区间 */ MCIWndPlayFromTo(hwnd, lStart, lEnd) /** 获取当前的播放进度 */ MCIWndGetPosition(hwnd) /** 获取窗口句柄所指对象文件的长度 */ MCIWndGetLength(hwnd) /** 销毁窗口句柄 */ MCIWndDestroy(hwnd) /** 设置播放音量 */ MCIWndSetVolume(hwnd,iVol) /** 获取当前播放音量 */ MCIWndGetVolume(hwnd) /** 设置播放速率 */ MCIWndSetSpeed(hwnd,iSpeed) /** 获取播放速率 */ MCIWndGetSpeed(hwnd) /** 设置是否循环播放 */ MCIWndSetRepeat(hwnd,f) /** 获取是否循环播放 */ MCIWndGetRepeat(hwnd)