The simplest LIBVLC-based example: The simplest LIBVLC-based video player (graphical interface version)

Source: Internet
Author: User

This document records a video player using a simple graphical interface developed by LIBVLC. Because it is a sample program, it contains only the simplest features of media playback. Some of these features are not perfect, and have time to make changes later.


The simplest LIBVLC-based video player (graphical interface version)

This is a sample player developed using LIBVLC 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 effect of the playback time is as follows.


The source code is long and is no longer recorded in detail. Simply record the implementation mechanism of several of these main functions.

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)Input URL converted to UTF-8 encoding (verbose record later)
(2)Provides a handle to the control that displays the video screen to LIBVLC
(3)Initialize LIBVLC and start playback
(4)Turn on the timer to update the video playback progress (detailed record later)
void Cplayerguidlg::onbnclickedstart () {CStringW cstr_url; #ifdef _unicodem_url. GetWindowText (cstr_url); #elseUSES_CONVERSION; CStringA Cstr_urla;m_url. GetWindowText (Cstr_urla); Cstr_url. Format (L "%s", a2w (Cstr_urla)); #endifstd:: String Str_url; Unicode_to_utf8 (Cstr_url, str_url); const char *CHAR_URL=STR_URL.C_STR (); if (strcmp (Char_url, "") ==0) {AfxMessageBox ( _t ("Input URL is null!"); return;} HWND Screen_hwnd=null;screen_hwnd = This->getdlgitem (idc_screen)->m_hwnd; if (playerstate!=state_prepare) {AfxMessageBox (_t ("Media is playing now"));     return;}     /* Create a new item *///m = libvlc_media_new_location (Libvlc_inst, "Http://mycool.movie.com/test.mov");     Libvlc_m = Libvlc_media_new_path (Libvlc_inst, Char_url);          /* Create A Media Player playing environement */libvlc_mp = Libvlc_media_player_new_from_media (libvlc_m);     /* No need to keep the media now */libvlc_media_release (libvlc_m);    On Windows Libvlc_media_player_set_hwnd (Libvlc_mp,screen_hwnd);  /* Play the Media_player */int x=libvlc_media_player_play (LIBVLC_MP); _sleep (30000); /* Let it play a bit */playerstate=state_play; SETBTN (State_play); SetTimer (1,1000,null);}

Pause/Resume Video The source code for pause/resume is as follows. where Libvlc_media_player_set_pause () is called to set "pause" or "continue".
void Cplayerguidlg::onbnclickedpause () {if (Playerstate==state_play) {libvlc_media_player_set_pause (libvlc_mp,1); Playerstate=state_pause; GetDlgItem (Id_pause)->setwindowtext (_t ("Resume"));} else if (playerstate==state_pause) {libvlc_media_player_set_pause (libvlc_mp,0);p layerstate=state_play; GetDlgItem (Id_pause)->setwindowtext (_t ("PAUSE"));}}

The source code to stop the video "Stop" is as follows. It calls Libvlc_media_player_stop () to stop video playback, and calls Libvlc_media_player_release () to release the corresponding libvlc_media_player_t struct body.
void Cplayerguidlg::onbnclickedstop () {if (libvlc_mp!=null) {libvlc_media_player_stop (LIBVLC_MP); Libvlc_media_ Player_release (LIBVLC_MP); KillTimer (1);} Systemclear ();}

Video playback Progress Miscellaneous timeline display 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, call Libvlc_media_player_get_time () to get the current video's playback progress, and also call Libvlc_media_player_get_length () to get the total length of the video.
After the calculation, you can set the result of the calculation to the corresponding edit box and the slide control bar (slider controls).
void Cplayerguidlg::ontimer (Uint_ptr nidevent) {if (nidevent = = 1) {CString Curtimestr,durationstr;int curtime;int Duration;int TNS, Thh, TMM, Tss;int progress;//mscurtime = Libvlc_media_player_get_time (LIBVLC_MP); if (curtime!=0) {// Change to Secondtns = Curtime/1000;thh  = tns/3600;tmm  = (TNS% 3600)/60;TSS  = (tns%); Curtimestr. Format (_t ("%02d:%02d:%02d"), THH,TMM,TSS); M_curtime. SetWindowText (CURTIMESTR);} Duration  = libvlc_media_player_get_length (LIBVLC_MP); if (duration!=0) {TNS = Duration/1000;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);}} Stop in the endif (Libvlc_media_player_get_state (LIBVLC_MP) ==libvlc_ended) onbnclickedstop (); 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 Libvlc_media_player_set_position () 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 posf=0.0;if (nsbcode==sb_thumbposition) {posf= (float) npos/100.0;libvlc_media_player_set_ Position (LIBVLC_MP,POSF);}} Cdialogex::onhscroll (Nsbcode, NPos, Pscrollbar);}

LIBVLC input file path problem in Chinese

LIBVLC is not a problem when using English as the input path. However, when we pass the Chinese path directly, there will be an issue where LIBVLC will parse the Chinese into garbled characters causing it to fail to play. The question stuck me for a while. The cause of this problem is that the input file path of VLC is encoded using UTF-8. So we need to transcode the input path to UTF-8 encoding. The problem is solved after transcoding.

The function code for Unicode transcoding to UTF-8 is shown below.
void Cplayerguidlg::unicode_to_utf8 (cstringw& unicodestring, std::string& str) {int stringlength =:: WideCharToMultiByte (Cp_utf8, NULL, unicodestring, Wcslen (unicodestring), NULL, 0, NULL, NULL); char* buffer = new CHAR[STR Inglength + 1];::widechartomultibyte (Cp_utf8, NULL, unicodestring, Wcslen (unicodestring), buffer, stringlength, NULL, NULL); Buffer[stringlength] = ' + '; str = buffer;delete[] buffer;

Download


Simplest LIBVLC Example


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

This project is comprised of some sample programs based on LIBVLC. The following sub-programs are included altogether.
Playergui: The simplest LIBVLC-based player-graphical interface version.
Simplest_libvlc_example: The simplest LIBVLC-based player.
Simplest_libvlc_streamer: The simplest LIBVLC-based push-to-flow device.

The simplest LIBVLC-based example: The simplest LIBVLC-based 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.