Click here to Skip to main content
15,921,694 members
Articles / Desktop Programming / MFC
Article

Simple Media Player

Rate me:
Please Sign up or sign in to vote.
2.71/5 (17 votes)
14 Feb 20051 min read 144.1K   6.5K   75   11
A simple media player.

Sample Image

Introduction

This program plays some audio/video (.avi, MPEG, MP3..) files. It uses DirectX 9.0 to play media files.

Software Requirements

Visual Studio .NET 2003, DirectX SDK 9.0.

Add the following libraries to the project:

Project->Properties->Linker->Input->Additional dependences
  • \\DXSDK\lib\strmiids.lib
  • \\DXSDK\Samples\C++\DirectShow\BaseClasses\Debug\strmbasd.lib

Instructions

Open a file by clicking Open button. Or open the playlist by pressing PlayList button. To add a file, click Add menu and Add File. You can select multiple files. Choose a file from playlist and click Play button on main window.

Classes

  • CWinPlayerDlg

    Class for main dialog.

  • CVideoWndDlg

    Dialog to display video.

  • CSeekSlider

    Class for the slider control.

  • CPlayList

    Class for list box to display the files to be played.

Playing media files

To render (output) a file, we need to instantiate various DirectX COM objects:

m_objHRsult=m_objGraphBuilder->QueryInterface (IID_IMediaControl, 
                                     (void**)&m_objMediaControl);
m_objHRsult=m_objGraphBuilder->QueryInterface (IID_IMediaEventEx, 
                                     (void**)&m_objMediaEvent);
m_objHRsult=m_objGraphBuilder->QueryInterface (IID_IVideoWindow, 
                                     (void**)&m_objVideownd);
m_objHRsult=m_objGraphBuilder->QueryInterface (IID_IMediaSeeking, 
                                     (void**)&m_objMediaSeeking);
m_objHRsult=m_objGraphBuilder->QueryInterface (IID_IMediaPosition, 
                                     (void**)&m_objMediaPos);
m_objHRsult= m_objGraphBuilder->QueryInterface(__uuidof(IVideoFrameStep), 
                                     (PVOID *)&m_objVideoFrame);

Give the reference to the owner window in which the file is displayed.

m_objVideownd->put_Owner((OAHWND) m_objVideoDlg.GetSafeHwnd());

To run the file:

m_objHRsult=m_objMediaControl->Run();

mfn_PauseClip()

To pause or play the clip, we check whether it is playing. If it is then:

m_objMediaControl->Pause()

pauses the Media control.

mfn_StepFrameFwd()

To move a clip forward to a certain number of frames, we do:

l_hResult=m_objVideoFrame->Step(15,NULL);

This moves 15 frames.

mfn_Close()

To close file:

l_hResult=m_objMediaControl->Stop();

Then to release all objects:

SAFE_RELEASE(m_objMediaEvent);
SAFE_RELEASE(m_objMediaPos);
SAFE_RELEASE(m_objMediaControl);
SAFE_RELEASE(m_objVideownd);
SAFE_RELEASE(m_objGraphBuilder);

Changing the position of slider:

Get current position:

DWORD dwPosition  = m_objSeekBar.GetPos();

Pause while scrolling:

m_objMediaControl->Pause();

Set new position:

REFERENCE_TIME newTime = (l_time/100) * dwPosition;
m_objMediaSeeking->SetPositions(&newTime, 
  AM_SEEKING_AbsolutePositioning, NULL, 
  AM_SEEKING_NoPositioning);

OnTimer()

Changes the position of slider control per second:

if (SUCCEEDED(m_objMediaSeeking->GetCurrentPosition(&timeNow)))
{
    l_PlayTime.Format("%d",timeNow/10000000);
    //ltoa((timeNow / 1000),l_PlayTime,10);
    m_objTimePlay.SetWindowText(l_PlayTime);
    long sliderTick = (long)((timeNow * 100) / l_time);
    m_objSeekBar.SetPos(sliderTick);
}

Acknowledgment

Code help is taken from DirectX SDK documentation. Guidance and support of Atin in this project is appreciable. Any suggestions and improvement in code are welcome. Further enhancement will be shortly available.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Working at a programmer in Jaipur(India) for TRYST Tec. Pvt. Ltd. on VC++ .I did my B.E (Computer Sc.).Now I am working on ASP.net.

Comments and Discussions

 
Generalexe file Pin
Member 81126535-Aug-11 23:52
Member 81126535-Aug-11 23:52 
GeneralMy vote of 1 Pin
ashar_0215-Dec-08 19:07
ashar_0215-Dec-08 19:07 
GeneralQue Pin
harshal718828-Sep-08 23:38
harshal718828-Sep-08 23:38 
GeneralWindow Media Player APi for Win2000 Machine Pin
preeti sharma18-Sep-08 3:56
preeti sharma18-Sep-08 3:56 
Generalcant change the play windows size Pin
googlelsg17-Sep-08 17:02
googlelsg17-Sep-08 17:02 
Generalthe project don't compile Pin
alphavn19-Nov-06 3:12
alphavn19-Nov-06 3:12 
GeneralRe: the project don't compile Pin
Abhishek Dutta11-Mar-07 5:33
Abhishek Dutta11-Mar-07 5:33 
QuestionHow to display a picture Pin
yanamandras20-Oct-06 21:43
yanamandras20-Oct-06 21:43 
Generalsearch code for visual c++ 6.0 enterprise edition Pin
fanomezana11-May-06 21:31
fanomezana11-May-06 21:31 
i search code for visual c++ 6.0 enterprise edition
QuestionHow to play DAT video files? Pin
reach2dpg2-Apr-06 5:28
reach2dpg2-Apr-06 5:28 
Generalvideo not showing Pin
grv57510-Jul-05 16:02
grv57510-Jul-05 16:02 

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.