Click here to Skip to main content
Licence 
First Posted 14 Feb 2005
Views 102,860
Bookmarked 74 times

Simple Media Player

By | 14 Feb 2005 | Article
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

About the Author

verma-rahul

Web Developer

India India

Member

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.

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalexe file PinmemberMember 811265323:52 5 Aug '11  
GeneralMy vote of 1 Pinmemberashar_0219:07 15 Dec '08  
GeneralQue Pinmemberharshal718823:38 28 Sep '08  
GeneralWindow Media Player APi for Win2000 Machine Pinmemberpreeti sharma3:56 18 Sep '08  
Generalcant change the play windows size Pinmembergooglelsg17:02 17 Sep '08  
Generalthe project don't compile Pinmemberalphavn3:12 19 Nov '06  
GeneralRe: the project don't compile PinmemberAbhishek Dutta5:33 11 Mar '07  
QuestionHow to display a picture Pinmemberyanamandras21:43 20 Oct '06  
Hi,
Interestingly simple. Thanks, but is there a way to simply take a picture (from an MPEG file/buffer) and display it?
 
thanks...
--Subbu
Generalsearch code for visual c++ 6.0 enterprise edition Pinmemberfanomezana21:31 11 May '06  
QuestionHow to play DAT video files? Pinmemberreach2dpg5:28 2 Apr '06  
Generalvideo not showing Pinmembergrv57516:02 10 Jul '05  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 14 Feb 2005
Article Copyright 2005 by verma-rahul
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid