C++ Hacker Programming: Development of Video Player

I haven't written for more than a year, so sorry everyone.

I wrote it in MFC, is it a bit ridiculous to use MFC in 2021

Not much nonsense, here is the project display picture:
I made it with the VLC library, you can download it on the official website, I have a Baidu network disk link:
**Link: https://pan.baidu.com/s/1ncgZC4mj35koQiJ5CVpZQg
Extraction Code: 6666
**

After downloading, there is a folder called "vlc", put it in the same directory as "xxx.sin", open VS, configure lib, include files, there are many tutorials on CSDN, if you don't know how to do it, you can search

! ! ! After configuring the library files, introduce several functions of the library

libvlc_media_player_stop: stop playing
libvlc_media_player_set_pause: pause or continue
libvlc_media_player_set_time: set playback time
libvlc_media_player_get_time: get playback time

For details, you can check the information on the official website

Controls of the main dialog box:
1. The EDIT control, which is the input box, is used to indicate the playback time, and the ID is IDC_EDIT1;
2. The progress bar, the variable m_slider, the ID is IDC_SLIDER1;
3. The pause button, the ID is IDC_BTN_PAUSE;

1. Open VS, create a new MFC project called "MyPlayer"
2. Configure VLC library
3. Delete useless codes, dialog boxes, etc. Add a dialog box, and add a dialog class associated with it, this dialog box is used to play video, the class is called "showdlg"
4. Create a new header file, a source file, "play.h" "play.cpp"

This is play.cpp

#include "Play.h"
#include "pch.h"
CPlay::CPlay() :
	m_pVlc_Inst(NULL), m_pVlc_media(NULL), m_pVlc_Player(NULL)
{
    
    

}

CPlay::~CPlay()
{
    
    
	
}

void CPlay::Init()
{
    
    
	if (m_pVlc_Inst == NULL)
		m_pVlc_Inst = libvlc_new(0, NULL);
}

void CPlay::Close()
{
    
    
    libvlc_media_player_stop(m_pVlc_Player);
}

void CPlay::Pause()
{
    
    
	libvlc_media_player_set_pause(m_pVlc_Player, 1);
}

void CPlay::Continue()
{
    
    
	libvlc_media_player_set_pause(m_pVlc_Player, 0);
}

BOOL CPlay::Play(char* szVedio,HWND hWnd)
{
    
    
	Init();
	if (szVedio == NULL || strlen(szVedio) == 0)
		return FALSE;
	m_pVlc_media = libvlc_media_new_path(m_pVlc_Inst, szVedio);
	m_pVlc_Player = libvlc_media_player_new_from_media(m_pVlc_media);
	libvlc_media_release(m_pVlc_media);
	libvlc_media_player_play(m_pVlc_Player);
	libvlc_media_player_set_hwnd(m_pVlc_Player, hWnd);

	return TRUE;
}

libvlc_time_t CPlay::GetLength()
{
    
    
	libvlc_media_parse(m_pVlc_media);
	return libvlc_media_get_duration(m_pVlc_media);
}

libvlc_time_t CPlay::GetCurrentPos()
{
    
    
	return libvlc_media_player_get_time(m_pVlc_Player);
}



void CPlay::MySetPos(float pos)
{
    
    
	libvlc_media_player_set_time(m_pVlc_Player, pos);
}

This is play.h

#ifndef  _PLAY_H
#define _PLAY_H
#include <vlc\vlc.h>


class CPlay
{
    
    
public:
	libvlc_instance_t* m_pVlc_Inst;
	libvlc_media_player_t* m_pVlc_Player;
	libvlc_media_t* m_pVlc_media;
public:
	CPlay();
	~CPlay();
	void Init();
	void Close();
	void Pause();
	void Continue();
	void MySetPos(float pos);
	BOOL Play(char* szVedio, HWND hWnd);
	libvlc_time_t GetLength();
	libvlc_time_t GetCurrentPos();
};

#endif 

This is the variable of MyPlayerDlg.h

    afx_msg void OnDropFiles(HDROP hDropInfo);
	CPlay m_video;
	BOOL m_bPlayState;//暂停的标识
	BOOL m_start;
	afx_msg void OnBnClickedBtnPause();
	CSliderCtrl m_slider;
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	CShowDlg* show = NULL;
	void MySetStatic();
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

This is the implementation of MyPlayerDlg.cpp

CMyPlayerDlg::CMyPlayerDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MYPLAYER_DIALOG, pParent)
{
    
    
	m_bPlayState = FALSE;
	m_start = FALSE;
}

void CMyPlayerDlg::OnDropFiles(HDROP hDropInfo)
{
    
    
	UINT nCount=DragQueryFile(hDropInfo,0xFFFFFFFF,NULL,NULL);
	TCHAR szVideoPath[MAX_PATH];
	DragQueryFile(hDropInfo, nCount-1, szVideoPath, MAX_PATH);
	wchar_t* b = new wchar_t[strlen(szVideoPath)];
	memset(b, 0, strlen(szVideoPath) * sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, szVideoPath, strlen(szVideoPath),b, strlen(szVideoPath)*sizeof(wchar_t));
	char* c = new char[wcslen(b) * 2];
	memset(c, 0, wcslen(b) * 2);
	WideCharToMultiByte(CP_UTF8, 0, b, wcslen(b), c, wcslen(b) * 2, NULL, NULL);
	CString strFilePath;
	strFilePath.Format("您打开的文件是%s,是否打开?", szVideoPath);
	if (MessageBox(strFilePath, "提示", MB_YESNO| MB_ICONQUESTION) == IDYES)
	{
    
    
		CString str;
		str.Format("当前播放视频为:%s", szVideoPath);
		SetDlgItemText(IDC_STATIC3, str);
		if (!show)
		{
    
    
			show = new CShowDlg;
			show->Create(IDD_DIALOG1, this);
			
		}
		show->ShowWindow(SW_SHOW);
		show->SetActiveWindow();
		m_video.Play(c,show->m_hWnd);
		int VedioLength= m_video.GetLength();
		m_slider.SetRange(0, VedioLength);
		SetTimer(1, 200, NULL);
		m_start = TRUE;
		m_bPlayState = TRUE;
	}
	delete[]c;
	delete[]b;
	CDialogEx::OnDropFiles(hDropInfo);
}

void CMyPlayerDlg::OnBnClickedBtnPause()
{
    
    
	
	if (m_start == FALSE)
	{
    
    
		MessageBox("请您拖放文件", "提示", MB_OK | MB_ICONERROR);
		return;
	}
	if (m_bPlayState == TRUE)
	{
    
    
		m_video.Pause();
		SetDlgItemText(IDC_BTN_PAUSE, "播放");
		m_bPlayState = FALSE;
	}
	else
	{
    
    
		m_video.Continue();
		SetDlgItemText(IDC_BTN_PAUSE, "暂停");
		m_bPlayState = TRUE;
	}
	
}


void CMyPlayerDlg::OnTimer(UINT_PTR nIDEvent)
{
    
    
	
	switch (nIDEvent)
	{
    
    
	case 1:
	   {
    
    
		int nPos = m_video.GetCurrentPos();
		CString strPos;
		int hour = 0;
		int minute = 0;
		int second = nPos / 1000;
		strPos.Format("%d:%d:%d", hour, minute, second);
		m_slider.SetPos(nPos);
		SetDlgItemText(IDC_EDIT1,strPos );
		  if (second >= 60)
		  {
    
    
			  minute = second/60;
			  second = second -(60*minute);
			  strPos.Format("%d:%d:%d", hour, minute, second);
			  SetDlgItemText(IDC_EDIT1, strPos);
		  }
		  
	   }
	}
	CDialogEx::OnTimer(nIDEvent);
}


void CMyPlayerDlg::MySetStatic()
{
    
    
	SetDlgItemText(IDC_STATIC3, "当前播放视频为:无");
	m_start = FALSE;
}


void CMyPlayerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    
    
	
	if (m_start == TRUE)
	{
    
    
		m_video.MySetPos((float)m_slider.GetPos());
	}
	else if (m_start == FALSE)
	{
    
    
		MessageBox("请您拖放文件", "提示", MB_OK | MB_ICONERROR);
		return;
	}
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

This is the close message function of ShowDlg.cpp, I overloaded it

void CShowDlg::OnClose()
{
    
    
	CDialogEx::OnClose();
	CMyPlayerDlg* str = (CMyPlayerDlg*)AfxGetMainWnd();
	str->MySetStatic();
	str->m_video.Close();
	str = NULL;
}

Compiled successfully on vs2019, please support me a lot. In terms of time processing, I did not handle the conversion of hours. If you are interested, you can try to modify it.
The method of use is to drag and drop the video file on the program

Guess you like

Origin blog.csdn.net/m0_47563648/article/details/118890822