Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

A Basic Media Player

, 10 Jan 2013 GPL3
Using MCIWnd wrapper class to write a simple media player
// mciDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mci.h"
#include "mciDlg.h"
#include "definition.h"
#include "easyplayer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CmciDlg dialog

IMPLEMENT_DYNAMIC(CmciDlg, CDialog);

CmciDlg::CmciDlg(CWnd* pParent /*=NULL*/)
: CDialog(CmciDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CmciDlg)
	m_PlayPauseStop = -1;
	m_Pos = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	iPlaying = PAUSE;
}

CmciDlg::~CmciDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
}

void CmciDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CmciDlg)
	DDX_Control(pDX, IDC_STATUS, c_Status);
	DDX_Control(pDX, IDC_SLIDER2, c_Volume);
	DDX_Control(pDX, IDC_STOP, c_Stop);
	DDX_Control(pDX, IDC_PAUSE, c_Pause);
	DDX_Control(pDX, IDC_PLAY, c_Play);
	DDX_Control(pDX, IDC_SLIDER1, c_Slider);
	DDX_Control(pDX, IDC_STATIC1, c_Static);
	DDX_Radio(pDX, IDC_PLAY, m_PlayPauseStop);
	DDX_Slider(pDX, IDC_SLIDER1, m_Pos);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CmciDlg, CDialog)
//{{AFX_MSG_MAP(CmciDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_COMMAND(ID_VIEW_FULLSCREEN, OnViewFullscreen)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_PLAYBACK_PLAYPAUSE, OnPlaybackPlayPause)
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_COMMAND(ID_FILE_EXIT, OnFileExit)
ON_WM_DESTROY()
ON_WM_TIMER()
ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
ON_COMMAND(ID_PLAYBACK_VOLUME_DOWN, OnPlaybackVolumeDown)
ON_COMMAND(ID_PLAYBACK_VOLUME_UP,   OnPlaybackVolumeUp)
	ON_COMMAND(ID_VIEW_PREFERENCES, OnViewPreferences)
	ON_COMMAND(ID_PLAYBACK_SEEK_REWIND, OnPlaybackSeekRewind)
	ON_COMMAND(ID_PLAYBACK_SEEK_FORWARD, OnPlaybackSeekForward)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CmciDlg message handlers

BOOL CmciDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	hPlay=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_PLAY));
	hPause=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_PAUSE));
	hStop=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_STOP));
	
	c_Play.SetIcon(hPlay);
	c_Pause.SetIcon(hPause);
	c_Stop.SetIcon(hStop);
	
	c_Play.EnableWindow(FALSE);
	c_Pause.EnableWindow(FALSE);
	c_Stop.EnableWindow(FALSE);
	
	lVol=rs.GetSettingsVolume();
	myPlayer.SetParent(c_Static.GetSafeHwnd());
	myPath="";
	myPlayer.Initialize();
	myPlayer.SetVolume(lVol);
	c_Volume.SetRange(0,1000,TRUE);
	c_Volume.SetPos(lVol);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CmciDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CmciDlg::OnPaint() 
{
	if (myPath=="")
	{
		CPen penBlue (PS_SOLID, 150, 0x00FF0000);
		CPen penRed (PS_SOLID, 150, 0x000000FF);
		CPen penYellow (PS_SOLID, 150, 0x0000FFFF);
		
		CPen* pOldPen;

		CRect rect;
		CRect rectBlack(500,500,5500,4000);
		
		c_Static.GetClientRect (&rect);
		
		CPaintDC dc ((CWnd*)&c_Static);
		dc.FillSolidRect(&rect, GetSysColor(COLOR_ACTIVEBORDER));
		dc.FillSolidRect(&rect, 0x00000000);
		//dc.Rectangle(500,500,5500,3500);
		
		
		dc.SetMapMode (MM_TWIPS);
		dc.SetViewportOrg (0 , rect.Height ());
		dc.SelectStockObject (NULL_BRUSH);
		
		pOldPen = dc.SelectObject (&penBlue);
		dc.SelectObject (&penRed);
		
		dc.Rectangle (1000,500,2500,1500);
		
		dc.SelectObject (&penYellow);
		dc.Rectangle (2000,1300,4500,3000);
		dc.Rectangle(4000,2700,5000,3300);
		
		dc.SelectObject (&penBlue);
		dc.Rectangle (1500,1700,4250,3700);
		dc.Rectangle(3000,800,4700,2200);
		
		dc.SelectObject (pOldPen);
	}

	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CmciDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CmciDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CmciDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CmciDlg::CanExit()
{
	return TRUE;
}

void CmciDlg::OnViewFullscreen() 
{
	if (myPath=="")
		return;
	
	CWinApp* pApp;
	pApp=AfxGetApp();
	
	KillTimer(ID_TIMER_POSITION);
	dlgFullSize.sPath=myPlayer.GetPath();
	dlgFullSize.lPos=myPlayer.GetPosition();
	dlgFullSize.iPlaying=iPlaying;
	dlgFullSize.lVol=lVol;
	
	myPlayer.Stop();
	myPlayer.Kill();
	
	
	if (dlgFullSize.DoModal())
	{
		myPlayer.SetParent(c_Static.GetSafeHwnd());
		myPlayer.SetPath(myPath);
		myPlayer.Initialize();
		
		lPos=dlgFullSize.lPos;
		c_Slider.SetPos(dlgFullSize.lPos);
		
		lVol=dlgFullSize.lVol;
		c_Volume.SetPos(dlgFullSize.lVol);		
		
		::ShowWindow(myPlayer.GetWindowHandle(),SW_MAXIMIZE); 	
		
		if (dlgFullSize.iPlaying==PLAY)
		{
			iPlaying=PLAY;
			myPlayer.Play();
			
			
			c_Play.EnableWindow(FALSE);
			c_Pause.EnableWindow(TRUE);
			c_Stop.EnableWindow(TRUE);
		}
		else if (dlgFullSize.iPlaying==PAUSE)
		{
			iPlaying=PAUSE;
			myPlayer.Pause();
			
			c_Play.EnableWindow(TRUE);
			c_Pause.EnableWindow(FALSE);
			c_Stop.EnableWindow(TRUE);	
		}
		else
		{
			iPlaying=STOP;
			myPlayer.Stop();
			
			c_Play.EnableWindow(TRUE);
			c_Pause.EnableWindow(FALSE);
			c_Stop.EnableWindow(FALSE);
		}
		
		SetTimer(ID_TIMER_POSITION,750,NULL);
	}
}

void CmciDlg::OnFileOpen() 
{
	TCHAR szFilter[]= _T("Video CD (*.dat)|*.dat|mp3 (*.mp3)|*.mp3|wav (*.wav)|*.wav|mpg (*.mpg)|*.mpg|All Files |*.*||");
	CFileDialog MediaFile(TRUE, _T("Video CD"), _T("*.dat"),
		OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_LONGNAMES,
		szFilter);
	
	TCHAR szTitle[]=_T("Open File");
	MediaFile.m_ofn.lpstrTitle=szTitle;
	
	int iResult;
	iResult=MediaFile.DoModal();
	if (iResult==IDOK)
	{	
		iPlaying=STOP;
		myPlayer.Stop();
		myPlayer.Close();
		
		myPath=MediaFile.GetPathName();
		myPlayer.SetPath(myPath);
		myPlayer.Initialize();		
		myPlayer.SetPosition(0);
		myPlayer.SetVolume(lVol);
		::ShowWindow(myPlayer.GetWindowHandle(),SW_MAXIMIZE); 	
		
		c_Play.EnableWindow(TRUE);
		
		c_Slider.SetRange(0,myPlayer.GetLength());
		c_Slider.SetPos(0);
		SetTimer(ID_TIMER_POSITION,500,NULL);

		if (rs.GetPlayAutoStart())
			OnPlaybackPlayPause();
	}	
}

void CmciDlg::OnPlaybackPlayPause() 
{
	if (myPath=="")
		return;
	
	if (iPlaying==PLAY)
	{
		iPlaying=PAUSE;
		myPlayer.Pause();
		
		c_Play.EnableWindow(TRUE);
		c_Pause.EnableWindow(FALSE);
		c_Stop.EnableWindow(TRUE);
	}
	else
	{
		iPlaying=PLAY;
		myPlayer.Play();
		
		c_Play.EnableWindow(FALSE);
		c_Pause.EnableWindow(TRUE);
		c_Stop.EnableWindow(TRUE);
	}
}

void CmciDlg::OnPlay() 
{
	m_PlayPauseStop=PLAY;
	iPlaying=PLAY;
	myPlayer.Play();
	
	c_Play.EnableWindow(FALSE);
	c_Pause.EnableWindow(TRUE);
	c_Stop.EnableWindow(TRUE);
}

void CmciDlg::OnPause() 
{
	m_PlayPauseStop=PAUSE;
	iPlaying=PAUSE;
	myPlayer.Pause();
	
	c_Play.EnableWindow(TRUE);
	c_Pause.EnableWindow(FALSE);
	c_Stop.EnableWindow(TRUE);
}

void CmciDlg::OnStop() 
{
	m_PlayPauseStop=STOP;
	iPlaying=STOP;
	myPlayer.Stop();
	myPlayer.SetPosition(0);
	
	c_Stop.EnableWindow(FALSE);
	c_Play.EnableWindow(TRUE);
	c_Pause.EnableWindow(FALSE);
}

void CmciDlg::OnFileExit() 
{
	OnOK();
}

void CmciDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	rs.SetSettingsVolume(lVol);
	KillTimer(ID_TIMER_POSITION);
	myPlayer.Kill();
}

void CmciDlg::OnTimer(UINT nIDEvent) 
{
	CWinApp* pApp;
	pApp=AfxGetApp();
	
	if (nIDEvent==ID_TIMER_POSITION)
	{
		lPos=myPlayer.GetPosition();
		
		m_Pos=lPos;
		UpdateData(FALSE);
		
		if (lPos==myPlayer.GetLength())
		{
			if(rs.GetPlayRewind())
				myPlayer.SetPosition(0);
			if (rs.GetPlayLoop())
				myPlayer.Play();
			else
				iPlaying=STOP;
		}

		CString sStatus;
		int iMin,iHour,iSec,iMinT,iHourT,iSecT;
		long lLen;
		lLen=myPlayer.GetLength();

		iHourT=(int)(lLen/3600000);
		lLen=lLen-3600000*iHourT;
		iMinT=(int)(lLen/60000);
		lLen=lLen-60000*iMinT;
		iSecT=(int)(lLen/1000);

		lLen=lPos;
		iHour=(int)(lLen/3600000);
		lLen=lLen-3600000*iHour;
		iMin=(int)(lLen/60000);
		lLen=lLen-60000*iMin;
		iSec=(int)(lLen/1000);

		sStatus.Format("%2d:%2d:%2d of %2d:%2d:%2d",iHour,iMin,iSec,iHourT,iMinT,iSecT);
		c_Status.SetWindowText(sStatus);
	}


	CDialog::OnTimer(nIDEvent);
}

void CmciDlg::OnFileClose() 
{
	myPlayer.Stop();
	myPlayer.Kill();
	iPlaying=STOP;
	
	c_Play.EnableWindow(FALSE);
	c_Pause.EnableWindow(FALSE);
	c_Stop.EnableWindow(FALSE);
	
	m_PlayPauseStop=-1;

	myPath="";
}

void CmciDlg::OnAppAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CmciDlg::OnCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult) 
{
	myPlayer.SetVolume(c_Volume.GetPos());
	lVol=myPlayer.GetVolume();
	
	*pResult = 0;
}

void CmciDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	myPlayer.SetPosition(c_Slider.GetPos());
	lPos=myPlayer.GetPosition();
	if (iPlaying==PLAY)
		myPlayer.Play();
	
	*pResult = 0;
}

void CmciDlg::OnPlaybackVolumeDown() 
{
	myPlayer.DecreaseVolume();
	c_Volume.SetPos(myPlayer.GetVolume());
	lVol=myPlayer.GetVolume();
}

void CmciDlg::OnPlaybackVolumeUp() 
{
	myPlayer.IncreaseVolume();
	c_Volume.SetPos(myPlayer.GetVolume());
	lVol=myPlayer.GetVolume();
}

void CmciDlg::OnOK() 
{
	CDialog::OnOK();
}

void CmciDlg::OnViewPreferences() 
{
	CPrefDlg dlgPref;
	dlgPref.DoModal();
}

void CmciDlg::OnPlaybackSeekRewind() 
{
	myPlayer.Rwd();
	if (iPlaying==PLAY)
		myPlayer.Play();
	OnTimer(ID_TIMER_POSITION);
}

void CmciDlg::OnPlaybackSeekForward() 
{
	myPlayer.Fwd();	
	if (iPlaying==PLAY)
		myPlayer.Play();
	OnTimer(ID_TIMER_POSITION);
}

BOOL CmciDlg::PreTranslateMessage(MSG* pMsg) 
{
	if (pMsg->message==WM_KEYDOWN)
	{
		if (pMsg->wParam==VK_ESCAPE)
			return TRUE;
		if (pMsg->wParam==VK_RETURN)
			return TRUE;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

M. Bonvari
Other
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 10 Jan 2013
Article Copyright 2003 by M. Bonvari
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid