The simplest example based on libVLC: the simplest libVLC-Based Video Player (GUI) and libvlc Gui

Source: Internet
Author: User

The simplest example based on libVLC: the simplest libVLC-Based Video Player (GUI) and libvlc Gui

========================================================== ==================

The simplest example of libVLC is as follows:

The simplest example is libVLC-based video player.

The simplest libVLC-based example: the simplest libVLC-Based Video Player (graphic interface Version)

The simplest example based on libVLC: the simplest libVLC-based streamer

========================================================== ==================

This document records a simple graphical video player developed using libVLC. The sample program only contains the simplest functions for media playback. Some of these functions are not complete, so you can modify them later.

 

The simplest libVLC-Based Video Player (graphic interface Version)

This is an example player developed based on MFC using libVLC. The basic functions of a player are as follows: Play, pause/continue, stop, display the playback timeline, and play media from any point. You can also drag a media file to the player for playback. Input the media file path to the URL bar before playing the video, and click Start to Start playing the video.


The following figure shows the effect during playback.


The source code is too long to be detailed. Briefly record the implementation mechanisms of several of the main functions.

The source code of the video "play" is as follows. In short, the initialization of the following video playback is completed:
(1) The input URL is converted to UTF-8 encoding (subsequent detailed records)
(2) provide the handle of the control that displays the video screen to libVLC.
(3) initialize libVLC and start playing
(4) Enable the timer to update the video playback progress (detailed 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);}

The source code for pausing/resuming a video is as follows. 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);playerstate=STATE_PLAY;GetDlgItem(ID_PAUSE)->SetWindowText(_T("Pause"));}}

The source code for stopping a video is as follows. Libvlc_media_player_stop () is called to stop video playback, and libvlc_media_player_release () is called to release the corresponding libvlc_media_player_t struct.
void CplayerGUIDlg::OnBnClickedStop(){if(libvlc_mp!=NULL){libvlc_media_player_stop (libvlc_mp);libvlc_media_player_release (libvlc_mp);KillTimer(1);}SystemClear();}

The timeline of the video playback progress. With the video playing, you must 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 enable the timer. The interval is set to 1000 ms.
SetTimer(1,1000,NULL);
Call KillTimer () to end the timer when the video is stopped.
KillTimer(1);
In the message response function of the timer, call libvlc_media_player_get_time () to obtain the playing progress of the current video. In addition, call libvlc_media_player_get_length () to obtain the total length of the video.
After calculation, you can set the calculation result to the corresponding Edit Control and Slider Control.
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 % 60);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 % 60);durationstr.Format(_T("%02d:%02d:%02d"),thh,tmm,tss);m_duration.SetWindowText(durationstr);progress=curtime*100/duration;m_progress.SetPos(progress);}}//Stop in the endif(libvlc_media_player_get_state(libvlc_mp)==libvlc_Ended)OnBnClickedStop();CDialogEx::OnTimer(nIDEvent);}


Adjust the video playback point. When you drag the Slider on the Slider Control, you need to set the video playback progress based on the drag position. Call libvlc_media_player_set_position () to set the video playback progress. 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);}

How to enter the path of a Chinese file in libVLC

LibVLC uses English as the input path. However, when we pass the Chinese path directly, libVLC resolves Chinese characters into garbled characters, leading to playback failure. This problem took me a while. This problem occurs because the input file path of the VLC is UTF-8 encoded. So we need to transcode the input path to UTF-8 encoding. After transcoding, this problem can be solved.

The function code for Unicode transcoding to UTF-8 is as follows.
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[stringLength + 1];::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), buffer, stringLength, NULL, NULL);buffer[stringLength] = '\0';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 contains some example programs based on libVLC. Contains the following subprograms.
PlayerGUI: the simplest libVLC-based player-Gui version.
Simplest_libvlc_example: the simplest libVLC-based player.
Simplest_libvlc_streamer: the simplest libVLC-based streamer.

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.