Click here to Skip to main content
Click here to Skip to main content

Media Player

By , 20 Sep 2003
 

Sample image

Sample image

Introduction

Media Player

This is a simple media player which plays all audio and video formats .I am updating it becouse there were many of many complaints about it . Actually i had not time to do it , now found some ,so updating it . It uses Media Control Interface(MCI) Functions in the MFC .It is very simple , that is we only have to give the path and file name and other parameters to the function and the function will play that file .

Description

First the file names are taken from the user using the file dialog .These file name are stored in an array of CString type Files[Index] ; Index is the the index of files .Multiple files can be selected at a time . Now to Create window of playing the file is created in the following  function

void CMP3Dlg::CreateWindowplay(CString str)
{
if(m_Video!=NULL)    // m_video is handler of HWND type which is 
			//checked wether it has already some window data 
OnFileClose();  
m_Path=str;
MCIWndRegisterClass();
KillTimer(0);
SetTimer(0,1000,NULL);   //  Set Timer 
m_Path=str;
if(m_Video == NULL &&flag==0)
{
m_Video =MCIWndCreate(m_screen.GetSafeHwnd(),AfxGetInstanceHandle()
,MCIWNDF_NOOPEN|MCIWNDF_NOPLAYBAR |MCIWNDF_NOMENU,m_Path);   
// Create the Window for playing m_Path  is file name
::ShowWindow(m_Video,SW_MAXIMIZE);
}
m_SVolume.SetRange( 0, 1000, TRUE );  // Set Slider range
m_SVolume.SetPos(500);   // Set volume Position initially 
m_mute.SetCheck(1);  // Set Checked the Volume check box 
ptr=FromHandle(m_Video);   // Get handler of Window created 
ptr->UpdateWindow();

MCIWndSetTimeFormat(m_Video ,"ms");   // Set time Format 
m_length=MCIWndGetLength(m_Video );   // Get Video Length
TotalTime=m_length/1000;
TotalTime=TotalTime/100;
m_Seek.SetRange( 0, m_length, TRUE );   // Set Seek slider bar range
m_Seek.SetPos(0);
MCIWndPlay(m_Video);   // Play the File
flag=1;
UpdateData(FALSE);
MCIWndSetVolume(m_Video, m_Volume);   // Set Volume to m_Volume 
e=0; // Check for previus created window
}

So now Our window is created and file is played . Now there is a option for pause that is

void CMP3Dlg::Onpause() 
{
// TODO: Add your control notification handler code here
if(flag==0)
{
flag=1;
MCIWndResume(m_Video);

}
else
{
flag=0;
MCIWndPause(m_Video);

}

}
to stop 
MCIWndSeek(m_Video,MCIWND_START);

To move to next file ,

OnFileClose() ;
SongNo++;
if(Files[SongNo]!="" )
{
CreateWindowplay(Files[SongNo]);  // again Create the window 
MCIWndPlay(m_Video);
flag=1;
}
else 
if(Index==0)
OnOpen();
else 
{
SongNo=0;
OnPlayPrevius() ;   // check if Index is full play previous 
}
}
The is a Implemenation for CD ROM door open and close . this is done as ,
// mciSendString("set cdaudio door open",NULL,NULL,NULL);

To Increase the Speed of Play

int a;
a=MCIWndGetSpeed(m_Video);
a=a+200;
if(a<2000)
MCIWndSetSpeed(m_Video,a);

Similarly there is a function to decrease the speed

For full screen option,  there is another dialog upon which the window is created . The dialog is maximized so full screen is got . It is done as

void CMP3Dlg::OnViewFullscreen() 
{
// TODO: Add your command handler code here
FullScreen=1;   // flag of full screen , Mode is Full screen
KillTimer(0);   // Kill timer
MCIWndDestroy(m_Video);  // destroy previous window 

// Now save the Context of Current file for full screen through FS object of 
//FullScreen Dialog
FS.SeekPos=SeekPos;   // Get seek position of previous window for new dialog 
 			//(Full screen)
FS.m_Volume=m_Volume;   // Get 
FS.m_Path=m_Path;
FS.DoModal();           // Create New dialog
SeekPos=FS.SeekPos;  // Return Back to Normal size and get New 
      			//Context from Full screen 
if(SeekPos==m_length )
{
if( repeat==1) 
{
OnPlayNext();    // Check whether the file has ended or seek is at end so 
                 //next file will also be played in the full //screen mode
Onfullscreen() ;
}
else
{
OnFileClose();
}

}
else
{
SetTimer(0,1000,NULL);

flag=1;   // Re-create Window for normal size play   
m_Video=MCIWndCreate(m_screen.GetSafeHwnd(),AfxGetInstanceHandle(),
MCIWNDF_NOOPEN|MCIWNDF_NOPLAYBAR|MCIWNDF_NOMENU ,m_Path);
::ShowWindow(m_Video,SW_MAXIMIZE);  // Restore the all Conext , volume value 
					//, seek postion ,,etc
MCIWndSetVolume(m_Video,m_Volume);
MCIWndSetTimeFormat(m_Video ,"ms");
MCIWndSetActiveTimer(m_Video,500);
ptr=FromHandle(m_Video);
m_length=MCIWndGetLength(m_Video); 
m_Seek.SetRange(0,m_length,TRUE);
MCIWndPlay(m_Video);
flag=1;
MCIWndSeek(m_Video,SeekPos);
MCIWndPlay(m_Video);
FullScreen=0;
ptr->UpdateWindow();
}
}
Now In new dialog Create Window and play according to previous context,
We will do it in the ShowWindow Function which is called first time when the
 dialog is created .
void FScreen::OnShowWindow(BOOL bShow, UINT nStatus) 
{
CDialog::OnShowWindow(bShow, nStatus);
CWnd* desk=GetDesktopWindow();  // Get the Desktop window's pointer
RECT re;
desk->GetClientRect(&re);
SetTimer(0,1000,NULL);
::ShowWindow(this->GetSafeHwnd(),SW_MAXIMIZE);   // Maximize window
// Now Create window for playing the file 
m_Video=MCIWndCreate(this->GetSafeHwnd(),AfxGetInstanceHandle(),
MCIWNDF_NOOPEN|MCIWNDF_NOPLAYBAR|MCIWNDF_NOMENU ,m_Path);
::ShowWindow(m_Video,SW_MAXIMIZE);  // Now set the previous saved context 
MCIWndSetTimeFormat(m_Video ,"ms");
MCIWndSetVolume(m_Video, m_Volume);
MCIWndSetActiveTimer(m_Video,500);
MCIWndSeek(m_Video,SeekPos);
MCIWndPlay(m_Video);
Length=MCIWndGetLength(m_Video);
}

We can add and remove  files to the Play list . There is another dialog for 
play list . 

To add files :

void playlist1::OnAdd() 
{
POSITION currPos;
CFileDialog avi(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY
,"All Files(*.*)|*.*|MP3 Files (*.mp3)|*.mp3|AVI Files(*.avi)|*.avi|");
if(avi.DoModal() == IDOK)
{
currPos = avi.GetStartPosition();
while(currPos != NULL)              // Get file names while now end of 
					//selection
{
m_Path = avi.GetNextPathName(currPos);
((CMP3Dlg *)GetParent())->Files[((CMP3Dlg*)GetParent())->m_Index]=m_Path;
m_control.AddString(m_Path);  // Add to List box
((CMP3Dlg*)GetParent())->m_Index=((CMP3Dlg*)GetParent())->m_Index+1;
}
UpdateData(FALSE);
}

}
Similary we can remove file through function 
void playlist1::OnRemove() 
{
// TODO: Add your control notification handler code here
i=m_control.GetCurSel();
m_control.DeleteString( i );
for(int j=0;j<=((CMP3Dlg*)GetParent())->m_Index;j++)
{
if(i==j)
{
((CMP3Dlg*)GetParent())->Files[i]=((CMP3Dlg*)GetParent())->Files[i+1];
for(int h=i;h<((CMP3Dlg*)GetParent())->m_Index;h++)
((CMP3Dlg*)GetParent())->Files[h]=((CMP3Dlg*)GetParent())->Files[h+1];
}
}
((CMP3Dlg*)GetParent())->m_Index--;

}

We can play a file  by double clicking on  File name

i=m_control.GetCurSel();
m_control.GetText(i,str);

if(((CMP3Dlg*)GetParent())->m_Video!=NULL)
{
MCIWndStop(((CMP3Dlg*)GetParent())->m_Video);
MCIWndDestroy(((CMP3Dlg*)GetParent())->m_Video);
((CMP3Dlg*)GetParent())->m_Video=NULL;
((CMP3Dlg*)GetParent())->m_Path=((CMP3Dlg*)GetParent())->OldFileName2; 
}
((CMP3Dlg*)GetParent())->NewFileName2=st;
((CMP3Dlg*)GetParent())->OldFilename=st;
((CMP3Dlg*)GetParent())->flag=0;
((CMP3Dlg*)GetParent())->CreateWindowplay(str);
The Shape of Dialog is got through
CRect r;
GetClientRect(r);
m_rgn.CreateEllipticRgn(04,04,r.Width(),r.Height());
SetWindowRgn(m_rgn,TRUE);

 

So thats all from me .What  could be done more in it ? that could be

1. We does not know about the file in the program ,whether its audio or video ,if audio to dont go to full screen

2. The full screen mechanism is not efficient , also it needs menu in full screen on right click .

3. Implementation of VCD cutter

and much more !!!!!!!!!!!!!

 


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tahir Naeem
Business Analyst
Germany Germany
Member
Tahir Naeem is a Business Analyst at a Software Company in Germany.
 
Presently he is working in the area of Business Intelligence.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugError while playing .wav filemembervijaykumar.soni22 Mar '13 - 0:07 
Hi Sir,
 
Thanks for posting very nice project.
 
I am facing problem while playing .wav files.
 
It gives error -
"MCI error" - "The driver cannot recognize the specified command"
 

Please help me to solve this error.
My email ID is vijaysoni23488@gmail.com
 
Thanks,
 
Vijay Soni Smile | :)
Questiondfdmemberprabhatjain4 Oct '12 - 15:22 
any one have dfd for this pls mail me prabhatjain01@gmail.com
l;ewefjdskl

Questiontoyoumemberzhangxiangxixi6 Dec '11 - 1:57 
so good!
Questionabout information about the referencesmemberMember 81126534 Aug '11 - 0:40 
sir iam Shrirang engineering student and in 5th sem.i am planning to make a media
player in c++ as my mini project. Would you kindly give information about the references i can take.And possibly the particular topics in c++ which
i have to study for the purpose.
Eagerly waiting for your reply.
Questionimplementationmembervinuvadakkemuriyil19 Jul '11 - 23:01 
sir am a beginner in the world of software.am doing my graduation.i have a dout in the case of implementation of the media player.how can i implement it if download the source code...............
GeneralAnaglyph/3d playermemberankireddy112231 Oct '10 - 20:34 
Hi Tahir,
 
It's very nice example.
I have two AVI's (Left and Right)now i want to play them both for 3D.
That is i need to create a player for stereoscopic.
Can you give any idea?
 
Thanks in advance
GeneralRunning the Projectmemberselonda19 Sep '10 - 22:44 
I got this error each time I try to compile ..
 

Error 1 error C2653: 'playlist' : is not a class or namespace name c:\users\administrator\desktop\mp3\playlist.cpp 20 Audio Player
 
and lots of other errors in the playlist.c file!!
 
what should i do?
Questionneed to build projectmembersatilo1 Aug '10 - 16:39 
sir iam a engineering student and in 5th sem.i am planning to make a media
player in c++ as my mini project. Would you kindly give information about the references i can take.And possibly the particular topics in c++ which
i have to study for the purpose.
Eagerly waiting for your reply.
rahul

GeneralDev-C++membersmely3 Dec '09 - 21:59 
Can You put it togeter for me and give me the download with the media player and its source code.
GeneralRe: Dev-C++memberTahir Naeem13 Mar '10 - 9:12 
its already togethor!
Tahir

GeneralNeed help on moving a line along with the audio slider positionmembervenki50218 Feb '09 - 7:08 
Hi Tahir,
I have created player application using MCI funtions, now i have one slider control and one image on the top of the dialog.
 
I want to move a vertical line along the slider position, and across the images.
I am able to move the slider position, but not able to move the line with respect to slider position.
 
Can you please help me regarding this..?
 
Thanks in advance..
Questionhow to integrate new codec into this player?memberSantosh Chaurasia30 Jul '08 - 4:12 
Is it possible to integrate my own mp3 code into the framework, instead of calling MCI libraries ?
AnswerRe: how to integrate new codec into this player?memberTahir Naeem30 Jul '08 - 5:31 
Yes, if you have the code and it works you can use the skelton code for creating the winodw.
 
Tahir

GeneralRe: how to integrate new codec into this player?memberSantosh Chaurasia30 Jul '08 - 5:45 
I have the code with its own API which is providing features of pause,play,forward,mute etc.
can u give me a clear idea of where mainly I have to change ur code, to insert my decoder code?
GeneralRe: how to integrate new codec into this player?memberTahir Naeem2 Aug '08 - 9:33 
Puhhhhhhhhhhh...
 
You need to understand the structure of a typical MFC application and then you can do it. Wink | ;)
 
I have no single line answer for it Frown | :(
 
Tahir

GeneralBUG !!!memberanmytry25 Jan '08 - 2:19 
I found a bug for "MCIWndGetLength" marco.
some length of mp3 can't be got accurately by "MCIWndGetLength".
I try to test many samples in CODEPROJECT , when I use the keyword "MP3 player" .
There are all the same bug in these samples. ~
 
oh ..no ~
 
hello

GeneralRe: BUG !!!memberTahir Naeem14 Feb '08 - 2:38 
Puhh...........
 

try to look for another API to get the length !!
 
I am sure there would be other API's to get the lenth !
 
Tahir

Questionkeyboard accelerator?memberm2sawyer27 Feb '07 - 4:24 
The code shows there is a keyboard accelerator: CTRL P (for play or pause)
But it doesn't work for me. I'm trying to add some more accelerators to it but without the one in the program working, I'm not sure what I need to fix.
Does it work for anyone else?
(Would it matter that on my computer, the project was converted to run on MS Visual C .Net 2003)
 
Thanks,
Mike
AnswerRe: keyboard accelerator?memberTahir Naeem28 Feb '07 - 7:55 
Sorry but there is no key board accelerator defined in this program !!
 
Tahir

GeneralMCI Errormembernaraimha2 Oct '06 - 23:42 
Confused | :confused: When i am running the avi file i am getting the MCI error(There is no Drive installed on your computer). But this application workingon Windows-xp os, when i am using Windows-2000 it is not working.
 
ranga
GeneralRe: MCI ErrormemberTahir Naeem3 Oct '06 - 1:22 
Yes, this is a problem. One need to register MCI to run in Windows 2K. And unfortunately i have not done that Frown | :(
 
Tahir

GeneralRe: MCI Errormembernaraimha3 Oct '06 - 2:47 
ok
Then How to solve this problem.
GeneralRe: MCI ErrormemberTahir Naeem3 Oct '06 - 2:51 
You have to register MCI class...How exactly ? sorry now idea Confused | :confused:
 
Tahir

Generalcant play VOB filessussluniv04048 Nov '04 - 2:23 
I have been searching for about a week or so to find a C++ VOB player. I have been unsuccessful in doing so. I would be nice if this media play could play VOB files.
 
Also do you know of anywhere I could find a VOB player that doesn’t skip when playing and has the capability to fast forward and rewind?
 
thanks
Chris
GeneralRe: cant play VOB filessussTahir Naeem8 Feb '05 - 0:34 
I dont have idea about VOB files but i think there would be some material helpfull in building an VOB player. For this purpose , a search has to be done in MSDN !
 
Tahir
GeneralErrormemberZero_Master9 Feb '04 - 20:40 
Hi Tahir
i download Download source code and run on VC++ 6.0
it error
============
LINK : fatal error LNK1104: cannot open file "libcmtd.lib"
Error executing link.exe.
============
what should i do? Dead | X|
 

GeneralRe: Errormemberlwinkenb24 Apr '04 - 11:24 
In the project settings, choose "Use MFC in a Shared DLL"
QuestionWindows Media Player example in win32?memberdobrin_s_ivanov@yahoo.com30 Oct '03 - 2:15 
Is there anybody that could write an Windows Media Player example in win32?
AnswerRe: Windows Media Player example in win32?sussAnonymous1 Dec '03 - 23:44 
That can found from many sites on internet by searching , might be availeble at www.codeguru.com
Big Grin | :-D
GeneralError in resourcememberFahadAsh22 Sep '03 - 19:09 
Hi,
First of all i should say this is nice effort by you, but DirectX is better idea.
 
anyway, your code is not working, due to not availablity of resource file. i found MP3.rc2 i renamed to .rc but some error.
 

Regards
Fahad
 
All mistakes done in life are experiences, and that should not be ignored
GeneralRe: Error in resourcememberTahir Naeem30 Sep '03 - 1:24 
Hi,
Thanks for your comments . I dont know why its not working at your machine becouse i dont have got complaint from any other guy also i have tested this code after downloading from codeproject.com .
i think there is some problom in your VC's installation .
U can try it after reinstalling the Visual Studio .
I hope that it will work .Smile | :)

 
Tahir
GeneralI wouldn't call this an article.memberlallous19 Jun '03 - 22:17 
Hello,
 
Consider my comment a constructive one...
 
This article shows nothing but a .jpg screenshot and 500kb zip file to be downloaded!
GeneralHelp regarding Skin and othersmemberAYSHA SAEED22 Feb '03 - 4:15 
Hi!
Tahir can you give some help on the skin implementation and also in the features of MPlayer like eject, visualization if i wanna add it along with songs like we normally can be with windows media player?
 
Bye.
 
Aysha Saeed
GeneralAakhir Bahrian Hi Ho Nan,Code Nahin DiamemberXclusiveGuy7 Feb '03 - 23:16 
Main Bhi Bahrian Hoon (BCS-V).
Barhi Khushi Kay Kisi Bahrian Nain Bhi Article Likha.
Keep It Up.
Best Of Luck.
 

 
The Malik
GeneralWelcome to CodeProject!!memberWREY3 Feb '03 - 23:47 
If I were to show up one day at your door with something that looks like a chair, and all I did was left a note asking, "Have you ever dreamt of going places and not having to worry about how to get there? Worry no more. Just sit in the chair and press the button."
 
Would you be suspicious of such an offer? Shoot! For all you know it could be an electric chair that's going to end your life!
 
The point is, without more explanation about what the product is all about, and the overall benefits it's supposed to provide, not a lot of people is going to buy into what you believe is a great piece of work.
 
Then in addition, to not providing the source code for those who might be interested in learning more about what you're talking about, puts you at zero with regards to what you originally thought others would find useful. It amounts to nothing!
 
Whatever you post here at CodeProject, belongs to you! Nobody is going to steal it. You own the copyright to it, and people at CodeProject would only be too happy to give you credit for your work should they decide to use it in some other application.
 
There are literally thousands of very good and useful samples here at CodeProject that members have provided for others to use (and benefit from), with only the request that the user give them some sort of credit for the usefulness of their work. That's it! That's all! And it's not unreasonable for others to comply with such a request either.
 
If you believe you have something worthwhile to share with the other members, then we welcome it. It is then for you to provide everything necessary, if benefit is to come from it, and benefit is not just a one way process: From you to us.
 
By submitting your source code, you will also gain from others suggesting or advising you on a better (or different) way of doing something, pointing out errors in your work, or directing you to some other article from which you, yourself might benefit.
 
Cool | :cool:
 
William
GeneralThis could be a really interesting article if...memberJim Crafton3 Feb '03 - 5:45 
You actually wrote an article!
A couple of lines of vague explanations are NOT an article, and I suspect that is at least part of the reason you have gotten such a low rating. I for one would be very interested to read more about this!
I hope you resubmit this after having fleshed it out a bit more.
 
Best of luck
 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
GeneralRe: This could be a really interesting article if...memberTahir Naeem4 Feb '03 - 6:38 
Hi ,
Yes , you rightly said that there was lack of explanation. thats why i got a poor grading . The reason is that i submitted the article in a hurry , so thats why it was not better .
But i hope that soon i'll re-submit the article with explanation .
Thanx for your openion .

 
Tahir
GeneralRe: This could be a really interesting article if...memberJim Crafton3 Jul '03 - 3:50 
Well IMHO you're much better wating and doing the article *right*, i.e. proper formatting, good content, developed thoughts, etc, than just rushing in and posting a sloppy, porrly developed article. And don't feel like I'm trying to single you out - I have seen a number of articles posted here on CP that suffer from the same issues, many of them far worse than yours. But please do update the article when you get the time!
 
Cheers
Jim
 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
GeneralRe: This could be a really interesting article if...memberTahir Naeem3 Jul '03 - 11:38 
Well jim ,
i know you are a person having positive thinking , i really dont mind your suggestions , first when you asked to update and give more illustration about the article, i did that becouse i know you was criticizing for improvement .
Thanks for your comment ,
Smile | :) Rose | [Rose]
 
Tahir
GeneralRe: This could be a really interesting article if...membernn_10524 Oct '05 - 9:12 
Nice work. You just have to polish the article. Keep doing what you are doing. In the end, its not
the article but the code thats important. Just make sure you document the code and don't worry
too much about the article.
 

Good luck
Questionwhere's the source?sussanonymousone2 Feb '03 - 20:56 
where's the source?
AnswerRe: where's the source?memberSimon Brown3 Feb '03 - 1:42 
Yes, the source code would be very interesting indeed.
 
If you can supply it then this would be a great contribution.
 
Simon HB9DRV
GeneralRe: where's the source?memberTahir Naeem27 Jul '03 - 12:18 
Now you can view it Smile | :) Cool | :cool:
 
Tahir

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 21 Sep 2003
Article Copyright 2003 by Tahir Naeem
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid