Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

A Basic Media Player

Rate me:
Please Sign up or sign in to vote.
4.45/5 (40 votes)
10 Jan 2013GPL33 min read 263.4K   12.7K   106   48
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
{
public:
CEasyPlayer();
CEasyPlayer(HWND hwParent);

public:
HWND hwndParent;

public:
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();

protected:
HWND c_Player;
CEvent m_Event;
CString m_sPath;

//{{AFX_MSG(CEasyPlayer)
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

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
not ready MCI_MODE_NOT_READY
open MCI_MODE_OPEN
pausedMCI_MODE_PAUSE
playingMCI_MODE_PLAY
recordingMCI_MODE_RECORD
seekingMCI_MODE_SEEK
stoppedMCI_MODE_STOP

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;
    pParameters=(CParams*)pParam;

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

    while(WaitForSingleObject(pEvent->m_hObject,5)==WAIT_TIMEOUT)
    {
        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. 

Acknowledgment 

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.  

License

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

 
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 
GeneralRe: set volume could not work Pin
zhangshsy9-Feb-06 15:27
zhangshsy9-Feb-06 15:27 
GeneralVideo Display Problem Pin
eVideoEuthusiast5-Feb-06 20:02
eVideoEuthusiast5-Feb-06 20:02 
Hi,

I have problem displaying the video files after compiling and executing this program. It appears that the video was flashed across the screen for less than a spilt second and then blank. I could still hear the audio.

Did anyone experience the same problem? Do we need to register the file types that can be played with MCI? If so, how can this be done?

The files that I was trying to playback are of mpeg and mpg format.
GeneralRe: Video Display Problem Pin
kezhu3-Apr-06 16:07
kezhu3-Apr-06 16:07 
GeneralRe: Video Display Problem Pin
kezhu3-Apr-06 16:21
kezhu3-Apr-06 16:21 
GeneralRe: Video Display Problem Pin
kezhu6-Apr-06 15:13
kezhu6-Apr-06 15:13 
GeneralRe: Video Display Problem Pin
Joseph Sid Faris13-Aug-06 10:51
Joseph Sid Faris13-Aug-06 10:51 
GeneralRe: Video Display Problem Pin
kezhu16-Aug-06 15:45
kezhu16-Aug-06 15:45 
GeneralSolution to Re:Video Display Problem Pin
Attila F9-Nov-09 19:21
Attila F9-Nov-09 19:21 
GeneralRe: Video Display Problem Pin
greenhello4-Oct-06 22:49
greenhello4-Oct-06 22:49 
Generalset position start and stop playing Pin
uumeme23-Dec-05 18:04
uumeme23-Dec-05 18:04 
GeneralRe: set position start and stop playing Pin
misugi24-Dec-05 17:49
misugi24-Dec-05 17:49 
GeneralRe: set position start and stop playing Pin
uumeme29-Dec-05 21:13
uumeme29-Dec-05 21:13 
QuestionChange position problem. Pin
Hongjun Ge10-Oct-05 15:28
Hongjun Ge10-Oct-05 15:28 
GeneralPrinting Pin
rturrentine22-Jun-05 3:42
rturrentine22-Jun-05 3:42 
QuestionHow to get the dialog self-refreshed? Pin
jason_azhe15-Mar-05 16:35
jason_azhe15-Mar-05 16:35 
AnswerRe: How to get the dialog self-refreshed? Pin
uumeme29-Dec-05 21:17
uumeme29-Dec-05 21:17 
Generalthis is crap! Pin
Yama_116-Oct-04 16:19
Yama_116-Oct-04 16:19 
GeneralRe: this is crap! Pin
Mehdi Bonvari29-Oct-04 19:43
Mehdi Bonvari29-Oct-04 19:43 
Generalpause and resume Pin
zhang lu4-Mar-04 21:13
zhang lu4-Mar-04 21:13 

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.