5,695,118 members and growing! (13,528 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » General     Intermediate License: The GNU General Public License (GPL)

A Basic Media Player

By M. Bonvari

Using MCIWnd wrapper class to write a simple media player
VC6, C++Windows, Win2K, WinXP, Win2003, MFC, VS6, Visual Studio, Dev

Posted: 8 Dec 2003
Updated: 8 Dec 2003
Views: 109,487
Bookmarked: 56 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 5.43 Rating: 3.68 out of 5
5 votes, 16.7%
1
1 vote, 3.3%
2
2 votes, 6.7%
3
4 votes, 13.3%
4
18 votes, 60.0%
5

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 mode MCI constant
not ready MCI_MODE_NOT_READY
open MCI_MODE_OPEN
paused MCI_MODE_PAUSE
playing MCI_MODE_PLAY
recording MCI_MODE_RECORD
seeking MCI_MODE_SEEK
stopped MCI_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!

Hasti 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.

This application is dedicated to my Hasti.

License

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

About the Author

M. Bonvari



Occupation: Other
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Audio and Video articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
GeneralMemory LeakmemberAmir Dashti21:21 8 Nov '08  
QuestionMedia player interfacememberboyooyo5:56 22 Sep '08  
GeneralMedia Playermemberjmahesh21:45 20 Aug '08  
QuestionmyPlayer.Play(), video plays very fastmembereverton10:32 19 Aug '08  
GeneralHow to catch the popup window?memberzhang lu0:28 26 Jul '07  
QuestionCan I draw over the same window?memberIgnacio Brana17:32 23 May '07  
Questionpros and consmemberNasreen.n.m20:23 4 Dec '06  
AnswerRe: pros and cons [modified]membertanvon malik18:40 21 Nov '07  
QuestiondirectshowmemberNasreen.n.m20:19 4 Dec '06  
AnswerRe: directshowmembertanvon malik18:35 21 Nov '07  
QuestionGet Position and Set FlagmemberYousuf Taufiq3:47 19 Jul '06  
Generalbad errormemberlaserbeak4311:40 30 May '06  
GeneralRe: bad errormemberM. Bonvari20:04 30 May '06  
Generalset volume could not workmemberzhangshsy15:44 9 Feb '06  
GeneralRe: set volume could not workmemberzhangshsy16:27 9 Feb '06  
GeneralVideo Display ProblemmembereVideoEuthusiast21:02 5 Feb '06  
GeneralRe: Video Display Problemmemberkezhu17:07 3 Apr '06  
GeneralRe: Video Display Problemmemberkezhu17:21 3 Apr '06  
GeneralRe: Video Display Problemmemberkezhu16:13 6 Apr '06  
GeneralRe: Video Display ProblemmemberJoseph Sid Faris11:51 13 Aug '06  
GeneralRe: Video Display Problemmemberkezhu16:45 16 Aug '06  
GeneralRe: Video Display Problemmembergreenhello23:49 4 Oct '06  
Generalset position start and stop playingmemberuumeme19:04 23 Dec '05  
GeneralRe: set position start and stop playingmembermisugi18:49 24 Dec '05  
GeneralRe: set position start and stop playingmemberuumeme22:13 29 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Dec 2003
Editor: Nishant Sivakumar
Copyright 2003 by M. Bonvari
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project