Click here to Skip to main content
15,664,435 members
Articles / Desktop Programming / MFC
Posted 8 Dec 2003


105 bookmarked

A Basic Media Player

Rate me:
Please Sign up or sign in to vote.
4.45/5 (40 votes)
10 Jan 2013GPL33 min read
Using MCIWnd wrapper class to write a simple media player

Sample Image - hmediaplayer.jpg

Introducting MCIWnd Window Class  

MCIWnd is a window class for controlling multimedia devices. A library of functions, messages, and macros associated with MCIWnd provides a simple method to add multimedia playback or recording capabilities to your applications.  

The MCIWnd Window class is very easy to use. By identifying a simple HWND member variable and assigning it to MCI window using a single function, you can create a control that plays any device that uses the media control interface (MCI) and gain support of any installed codec on your machine. These devices include Audio CDs, WAV Audios, MIDIs, and video devices with any supported codec.

Automating playback is also quick and easy. Using only one function and two macros, an application can create an MCIWnd window with the appropriate media device, play the device, and close both the device and the window when the content has finished playing.

Note that term device refers to any playable media from file stored to any playable medium.

Note: to use MCIWnd Class in your application you must add vfw32.lib library to Link sections of Project Settings (accessible by Ctrl+F7) or simply add following line your header:

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

which does the same.

Considering we want to write a dialog based player, after adding library, add a HWND member variable to your dialog class (as mentioned above), for example:

HWND m_Player;

Then, add following code to any section you want to open your media for playing:

m_Player = MCIWndCreate(GetSafeHwnd(),AfxGetInstanceHandle(),
    WS_CHILD | WS_VISIBLE, m_sFileName);

Where m_sFileName is a member variable which holds media name. After that you can use single line macros to control media playback. e.g. MCIWndPlay(m_Player) to start media playback or MCIWndStop(m_Player).

You are done, you have a media player now!

A wrapper class for MCIWnd: CEasyPlayer 

I have written a very simple wrapper class for this API. This wrapper class is part of my work. That certain program needed simple playback of wav and mp3 media and sometimes continuous playing (loop). So, I just wrapped very few macros and messages in implementing the class. The class interface is as below:

class CEasyPlayer : public CWnd
CEasyPlayer(HWND hwParent);

HWND hwndParent;

long lPos;
void Rwd();
void Fwd();
void DecreaseVolume();
void IncreaseVolume();
long GetVolume();
long lVolume;
void SetVolume(long lVol);
long GetLength();
long GetPosition();
void SetPosition(long lPos);
HWND GetWindowHandle();
void Resume();
void Kill();
void Break();
void Loop();
void Close();
HWND Initialize();
void Play();
void Stop();
void Pause();
long GetMode();
CString GetPath();
void SetPath(CString sPath);
void SetParent(HWND hParent);
HWND GetParent();
virtual ~CEasyPlayer();

HWND c_Player;
CEvent m_Event;
CString m_sPath;

afx_msg void OnDestroy();

c_Player is that so called HWND member variable. I use c_ prefix to determine control variables from other member variables(Values). Initialize() creates MCIWnd control and assigns it to c_Player. Other functions are identify themselves by their names which show exact duty of function. It is necessary to mention that GetMode() is used to determine playback status and returns one of following values:

Operating modeMCI constant

Class code is clear, it just wraps some macros, passes values to them and gets results from them. The only part of code that may need description is Loop() function. Using Loop() will cause launching a thread call to LoopThread() which implementation is as follows:

UINT LoopThread(LPVOID pParam)
    CParams* pParameters;

    CEasyPlayer* pWnd=(CEasyPlayer*)pParameters->pWnd;
    CEvent* pEvent=(CEvent*)pParameters->pEvent;
    HWND c_Player=(HWND)pParameters->hWnd;

        if (pWnd->GetMode()==MCI_MODE_STOP)
            ::PostMessage(c_Player, MCI_PLAY, 0, 0);
    return 0;

A conditional loop which checks if m_Event member variable of class is set, if not then checks if media playback stoped. If so then replys the media by sending a MCI_PLAY message to HWND control. Very simple!

Media Player

After concluding that project, I decided to try writing a Media Player, and that'is it. Using potentials of CStatic control as a CWnd derived class, I made a GUI for my wrapper class. I used a CStatic control to hold onto video media playback (if exist). This CStatic control rules as CEasyPlayer instance parent.

Changing approach this application did not used Loop() member function for continuous media playback. Instead a timer used to periodically check if media stopped or not. 


I must mention that I used Nishant S approach to handles key board messages and menu accelerators in this application. Thank you very much Nishant S. 

I took the introduction section of the article from MSDN (^). This article is first written in 2003.  


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Written By
Chief Technology Officer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 4 Pin
Jiří Miklík4-Jun-13 1:30
Jiří Miklík4-Jun-13 1:30 
GeneralMessage Closed Pin
20-May-16 20:37
professionalhi reddit20-May-16 20:37 
Questionmedia player vb Pin
strbry11-Jan-13 20:42
strbry11-Jan-13 20:42 
QuestionBefore I vote ... Pin
Chris Meech9-Jan-13 5:35
Chris Meech9-Jan-13 5:35 
AnswerRe: Before I vote ... Pin
Mehdi Bonvari10-Jan-13 0:59
Mehdi Bonvari10-Jan-13 0:59 
GeneralRe: Before I vote ... Pin
Chris Meech10-Jan-13 3:15
Chris Meech10-Jan-13 3:15 
AnswerRe: Before I vote ... Pin
DaveAuld10-Jan-13 1:00
professionalDaveAuld10-Jan-13 1:00 
GeneralRe: Before I vote ... Pin
Chris Meech10-Jan-13 3:11
Chris Meech10-Jan-13 3:11 
GeneralProblem with MCIWndGetPosition(c_Player); Pin
James86XXX3-Oct-09 3:27
James86XXX3-Oct-09 3:27 
GeneralMCIWndOpen() Pin
zhang lu12-Apr-09 0:31
zhang lu12-Apr-09 0:31 
Generalwhy lVol=rs.GetSettingsVolume()can not get correct result Pin
zjutyujf16-Mar-09 22:10
zjutyujf16-Mar-09 22:10 
GeneralMemory Leak Pin
Amir Dashti8-Nov-08 20:21
professionalAmir Dashti8-Nov-08 20:21 
QuestionMedia player interface Pin
boyooyo22-Sep-08 4:56
boyooyo22-Sep-08 4:56 
Help me please. I want to design my own media player. I've designed an interface with photoshop. I've also written a player with a very ugly interface. I use ordinary buttons for the play, stop etc commands.

What I want to know is. How can I use the interface I've designed in photoshop in my application so that my player will look and feel like a real media player?

Thanks in advance
GeneralMedia Player Pin
jmahesh20-Aug-08 20:45
jmahesh20-Aug-08 20:45 
QuestionmyPlayer.Play(), video plays very fast Pin
everton19-Aug-08 9:32
everton19-Aug-08 9:32 
QuestionHow to catch the popup window? Pin
zhang lu25-Jul-07 23:28
zhang lu25-Jul-07 23:28 
QuestionCan I draw over the same window? Pin
IBrana23-May-07 16:32
IBrana23-May-07 16:32 
Questionpros and cons Pin
Nasreen.n.m4-Dec-06 19:23
Nasreen.n.m4-Dec-06 19:23 
AnswerRe: pros and cons [modified] Pin
tanvon malik21-Nov-07 17:40
tanvon malik21-Nov-07 17:40 
Questiondirectshow Pin
Nasreen.n.m4-Dec-06 19:19
Nasreen.n.m4-Dec-06 19:19 
AnswerRe: directshow Pin
tanvon malik21-Nov-07 17:35
tanvon malik21-Nov-07 17:35 
QuestionGet Position and Set Flag Pin
Yousuf Taufiq19-Jul-06 2:47
Yousuf Taufiq19-Jul-06 2:47 
Generalbad error Pin
laserbeak4330-May-06 10:40
laserbeak4330-May-06 10:40 
GeneralRe: bad error Pin
Mehdi Bonvari30-May-06 19:04
Mehdi Bonvari30-May-06 19:04 
Generalset volume could not work Pin
zhangshsy9-Feb-06 14:44
zhangshsy9-Feb-06 14:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.