这是使用libVLC基于MFC开发的一个示例播放器。实现了一个播放器的基本功能:播放,暂停/继续,停止,播放时间轴的显示,以及从任一点开始播放媒体。并且支持将媒体文件拖拽至播放器进行播放。播放前将媒体文件的路径输入到“URL”栏中,然后单击“Start”即可开始播放。
播放时候的效果截图如下所示。
源代码比较长,不再详细记录。简单记录一下其中几个主要功能的实现机制。
void CplayerGUIDlg::OnBnClickedStart() { CStringW cstr_url; #ifdef _UNICODE m_url.GetWindowText(cstr_url); #else USES_CONVERSION; CStringA cstr_urla; m_url.GetWindowText(cstr_urla); cstr_url.Format(L"%s",A2W(cstr_urla)); #endif std::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); }
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")); } }
void CplayerGUIDlg::OnBnClickedStop() { if(libvlc_mp!=NULL){ libvlc_media_player_stop (libvlc_mp); libvlc_media_player_release (libvlc_mp); KillTimer(1); } SystemClear(); }
SetTimer(1,1000,NULL);在视频停止播放的时候,调用KillTimer()结束定时器。
KillTimer(1);在定时器的消息响应函数中,调用libvlc_media_player_get_time()获取当前视频的播放进度,此外调用libvlc_media_player_get_length()获取视频的总时长。
void CplayerGUIDlg::OnTimer(UINT_PTR nIDEvent) { if (nIDEvent == 1){ CString curtimestr,durationstr; int curtime; int duration; int tns, thh, tmm, tss; int progress; //ms curtime = libvlc_media_player_get_time(libvlc_mp); if(curtime!=0){ //change to second tns = 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 end if(libvlc_media_player_get_state(libvlc_mp)==libvlc_Ended) OnBnClickedStop(); CDialogEx::OnTimer(nIDEvent); }
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使用英文作为输入路径的时候是没有问题的。但是当我们直接传递中文路径的时候,会出现libVLC将中文解析为乱码从而导致无法播放的问题。这个问题卡了我一阵子时间。造成这一问题的原因在于VLC的输入文件路径是使用UTF-8编码的。因此我们需要将输入路径转码为UTF-8编码。转码之后这一问题即得到了解决。
Unicode转码为UTF-8的函数代码如下所示。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; }
Simplest libVLC Example
最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器(图形界面版)
原文地址:http://blog.csdn.net/leixiaohua1020/article/details/42363593