Simplest DirectShow-based example: Video player graphical interface version

Source: Internet
Author: User

This document records one of the simplest video players based on the DirectShow graphical interface. The example of a DirectShow-based graphical interface player is still more, but most of them are "layer-wrapped" examples. The "Layered encapsulation" example is relatively more stable, but it is not very easy to understand. Because the number of interface functions of the DirectShow itself is more, if combined with the function of encapsulating DirectShow, the number of functions combined is very large, it is easy to confuse which is the real DirectShow interface function. This player stripped the DirectShow example of "layer encapsulation", directly call DirectShow interface to complete the video playback work, more suitable for the DirectShow to use.


Several functions of the implementation mechanism of the entire project code is more, no longer detailed records. A few key points in the code are briefly documented here.
Video playback/pause/Resume/Stop playing video The source code for "Play" is shown below. In a nutshell, the following video playback initialization is done:
(1)The input URL is converted to Unicode encoding (the input supported by the RenderFile () function is a Unicode string).
(2)Call RenderFile () "smart" to create the filter Graph.
(3)Call IMediaControl's Run () method to start playing the video.
(4)Turn on the timer to update the video playback progress (detailed record later)
void Cplayerguidlg::onbnclickedstart () {CStringA Cstr_urla; CStringW CSTR_URLW; HRESULT hr;//render#ifdef _unicodem_url. GetWindowText (CSTR_URLW); #elseUSES_CONVERSION; M_url. GetWindowText (Cstr_urla); cstr_urlw. Format (L "%s", a2w (Cstr_urla)); #endifif (CSTR_URLW. IsEmpty ()) {AfxMessageBox (_t ("Input URL is null!")); return;} hr = Pgraph->renderfile (CSTR_URLW, NULL), if (FAILED (HR)) {AfxMessageBox (_t ("Can ' T Open input file!"); return;} Set Windowhwnd Screen_hwnd=null; RECT Windowrect;screen_hwnd = This->getdlgitem (idc_screen)->getsafehwnd (); :: GetClientRect (Screen_hwnd, &windowrect);p window->put_visible (oafalse);p Window->put_owner ((OAHWND) Screen_hwnd);p window->put_left (0);p window->put_top (0);p window->put_width (Windowrect.right- Windowrect.left);p window->put_height (windowrect.bottom-windowrect.top);p Window->put_windowstyle (WS_CHILD | ws_clipchildren| ws_clipsiblings| Ws_thickframe);p Window->put_messagedrain ((Oahwnd) screen_hwnd);//receive messagepwindow->put_visible (OAtrue);p Event->setnotifywindow ((Oahwnd) Screen_hwnd, wm_graphnotify, 0);//runhr = Pcontrol->run ();p layerstate =state_play; SETBTN (State_play); SetTimer (1,1000,null);}

Pause/Resume Video The source code for pause/resume is as follows. Pause () and run () of IMediaControl are called to set "suspend" or "continue".
void Cplayerguidlg::onbnclickedpause () {HRESULT hr;if (playerstate==state_play) {hr=pcontrol->pause (); Playerstate=state_pause; GetDlgItem (Id_pause)->setwindowtext (_t ("Resume"));} else if (playerstate==state_pause) {hr=pcontrol->run ();p layerstate=state_play; GetDlgItem (Id_pause)->setwindowtext (_t ("PAUSE"));}}

The "Stop" source code for the stop video is shown below. This part of the code completes the following work:
(1)Re-adjust the playback position to 0
(2)Call IMediaControl's Pause ()
(3)Turn off the timer
(4)Delete filter in filter graph
void Cplayerguidlg::onbnclickedstop () {long Long position = 0; HRESULT hr;hr = pseeking->setpositions (&position, am_seeking_absolutepositioning | Am_seeking_seektokeyframe, 0, am_seeking_nopositioning); KillTimer (1); Hr=pcontrol->stop ();//Enumerate the filters and remove themienumfilters *penum = NULL;HR = pgraph->en Umfilters (&penum); if (SUCCEEDED (HR)) {Ibasefilter *pfilter = null;while (S_OK = = Penum->next (1, &pfilter, NULL) {//Remove the Filter.pgraph->removefilter (pfilter);//Reset the Enumerator.penum->reset ();p filter-> Release ();} Penum->release ();} Systemclear ();}

Video playback progress is displayed in the timeline as the video plays, you need to update the playback progress information on the timeline of the video playback progress. A timer is used in the program to complete this function.
When the video starts playing, call SetTimer () to turn on the timer. The time interval is set to 1000ms.
SetTimer (1,1000,null);
When the video stops playing, call the KillTimer () end timer.
KillTimer (1);
In the message response function of the timer, the imediaseeking getcurrentposition () is called to get the time that the video is currently playing, calling Imediaseeking's Getduration () to get the length of the video. Based on the values obtained from the above function, the result is set to the corresponding control after the calculation. The code for this section is shown below.
void Cplayerguidlg::ontimer (Uint_ptr nidevent) {if (nidevent = = 1) {CString curtimestr,durationstr;long long curtime; Long Long duration;int TNS, Thh, TMM, Tss;int progress;//mspseeking->getcurrentposition (&curtime); if (curtime!= 0) {//change to Secondtns = curtime/10000000;thh  = tns/3600;tmm  = (TNS% 3600)/60;TSS  = (tns%); curtimes Tr. Format (_t ("%02d:%02d:%02d"), THH,TMM,TSS); M_curtime. SetWindowText (CURTIMESTR);} Pseeking->getduration (&duration); if (duration!=0) {TNS = Duration/10000000;thh  = tns/3600;tmm  = (TNS% 3600)/60;tss  = (TNS%);d Urationstr. Format (_t ("%02d:%02d:%02d"), THH,TMM,TSS); m_duration. SetWindowText (DURATIONSTR);p rogress=curtime*100/duration;m_progress. SetPos (progress);}} Cdialogex::ontimer (nidevent);}


Adjusting the video playback point when you drag the slider on the slide control control, you need to set the playback progress of the video according to the position you dragged. This calls Imediaseeking's Setpositions () to set the playback progress of the video. The code in the message response function is as follows.
void Cplayerguidlg::onhscroll (UINT nsbcode, uint NPos, cscrollbar* pscrollbar) {if (Pscrollbar->getsafehwnd () = = M_ Progress. GetSafeHwnd ()) {float Pos_bar=0.0;long long Duration=0.0;long long pos_time=0.0;if (nsbcode==sb_thumbposition) {Pos_ Bar= (float) npos/100.0;pseeking->getduration (&duration);p os_time=pos_bar*duration;long Long position = (long Long) (pos_time); HRESULT hr = pseeking->setpositions (&position, am_seeking_absolutepositioning | Am_seeking_seektokeyframe, 0, am_seeking_nopositioning);}} Cdialogex::onhscroll (Nsbcode, NPos, Pscrollbar);}

Full screen playback of the problem video is implemented via Ivideowindow's Put_fullscreenmode (), and the code is shown below.
void Cplayerguidlg::onbnclickedfullscreen () {pwindow->put_fullscreenmode (oatrue);}

Also, after full screen mode is started, you can turn off full screen mode if you press the ESC key. This part of the code is implemented in PreTranslateMessage (), as shown below.
Exit Full Screen mode when push "ESC" BOOL Cplayerguidlg::P retranslatemessage (msg* pMsg) {if (Pmsg->message = = Wm_keyd OWN) {if (Pmsg->wparam = = Vk_return | | pmsg->wparam = = vk_escape) {//Restore form fullscreen modepwindow->put_ful Lscreenmode (Oafalse); return 1;}} return CDialogEx::P retranslatemessage (PMSG);}

One thing to note here is that Ivideowindow's Put_fullscreenmode () is problematic under Win7. Only when you set the window style, you specify Ws_thickframe in the style to use it normally. For example, the following code.
Pwindow->put_windowstyle (ws_child| ws_clipchildren| ws_clipsiblings| Ws_thickframe);
If you do not specify the Ws_thickframe style, after exiting "full screen" mode, the video will not appear, instead of a piece of black.

However, after setting the Ws_thickframe style, there will be a "white edge" on the periphery of the video window, which will affect the appearance of the video display. So if we want to use the full screen properly, we may need to find a better way, where I have not studied in depth.


Run results

This is a sample player developed using DirectShow based MFC. Implements the basic functions of a player: play, pause/resume, stop, play the timeline display, and start playing media from any one o'clock. The media files are also supported to be dragged to the player for playback. Before playing, enter the path of the media file into the URL bar and click Start to start playback. The "Start", "Pause", and "Stop" buttons are included under the software to control the playback of the media.


The effect of the playback time is as follows.


Click "Full Screen" to play in Fullscreen. Click Info to display information about the media that is playing, including the following two types of information:
(1)Information about this video
(2)Play the filter in the filter graph of the video.

Download simplest DirectShow Example

SourceForge Project home: https://sourceforge.net/projects/simplestdirectshowexample/
cdsn:http://download.csdn.net/detail/leixiaohua1020/8348163

This program contains a sample program developed by DirectShow. Suitable for DirectShow beginners to learn.
It contains the following sub-programs:
Simplest_directshow_player: The simplest DirectShow-based video player.
Simplest_directshow_player_custom: The simplest DirectShow-based video player (custom).
Playergui: The simplest DirectShow-based player-graphical interface version.
Simplest_directshow_info: The simplest example of DirectShow information display.
Simplest_directshow_filter: Not yet completed.



Simplest DirectShow-based example: Video player graphical interface version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.