65.9K
CodeProject is changing. Read more.
Home

Simple Media Player

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.71/5 (17 votes)

Feb 14, 2005

1 min read

viewsIcon

144563

downloadIcon

6521

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.