qt+libvlc实现简单播放器

播放器功能及效果图

  • 文件选择
  • 开始、暂停、停止
  • 倍率
  • 进度条显示
  • 文件定位播放
  • 支持鼠标双击全屏

libvlc环境

  • 下载网址

Index of /pub/videolan/vlc/last/

  • 解压缩后sdk目录为开发目录,将plugins目录及libvlc.dll、libvlccore.dll放到运行环境下

代码实现简单播放器

  • 包含LIBVLC头文件

        包含vlc.h之前需先定义ssize_t,否则会编译错误

typedef __int64 ssize_t;
#include "vlc/vlc.h"
  • 链接库

  • 代码实现

class VLCPlayer : public QWidget
{
	Q_OBJECT

public:
	VLCPlayer(QWidget *parent = nullptr);
	~VLCPlayer();

	Q_INVOKABLE void onVlcMediaPlayerPlaying();
	Q_INVOKABLE void onVlcMediaPlayerTimeChanged();
	Q_INVOKABLE void onVlcMediaPlayerEndReached();
	Q_INVOKABLE void onVlcMediaPlayerStopped();


private Q_SLOTS:
	void on_btnOpen_clicked();
	void on_btnPlay_clicked();
	void on_btnStop_clicked();

	void on_horizontalSliderPlayer_valueChanged(int value);

	void on_comboBoxRate_currentTextChanged(const QString& text);

protected:
	void resizeEvent(QResizeEvent* event);
	void mouseDoubleClickEvent(QMouseEvent* event);

private:
	static void vlcEvents(const libvlc_event_t* ev, void* param);

	void setVideoAspectRatio(bool aspectRatio = true);

private:
	Ui::VLCPlayerClass *ui;

	libvlc_instance_t*		m_vlcBase = nullptr;
	libvlc_media_t*			m_vlcMedia = nullptr;
	libvlc_media_player_t*	m_vlcMediaPlayer = nullptr;
};

VLCPlayer::VLCPlayer(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::VLCPlayerClass())
{
	ui->setupUi(this);

	m_vlcBase = libvlc_new(0, NULL);
	Q_ASSERT(m_vlcBase);
}

VLCPlayer::~VLCPlayer()
{
	if (m_vlcBase) {
		libvlc_release(m_vlcBase);
		m_vlcBase = nullptr;
	}

	delete ui;
}

void VLCPlayer::onVlcMediaPlayerPlaying()
{
	if (m_vlcMediaPlayer)
	{
		libvlc_time_t duration = libvlc_media_player_get_length(m_vlcMediaPlayer);

		QTime time = QTime(0, 0, 0).addMSecs(duration);
		QString strTime = time.toString("mm:ss");
		ui->labelEndTime->setText(strTime);

		ui->horizontalSliderPlayer->setMaximum(duration / 1000);
	}
}

void VLCPlayer::onVlcMediaPlayerTimeChanged()
{
	if (m_vlcMediaPlayer)
	{
		libvlc_time_t curTime = libvlc_media_player_get_time(m_vlcMediaPlayer);

		QTime time = QTime(0, 0, 0).addMSecs(curTime);
		QString strTime = time.toString("mm:ss");
		ui->labelStartTime->setText(strTime);

		ui->horizontalSliderPlayer->blockSignals(true);
		ui->horizontalSliderPlayer->setValue(curTime / 1000);
		ui->horizontalSliderPlayer->blockSignals(false);
	}
}

void VLCPlayer::onVlcMediaPlayerEndReached()
{
	if (m_vlcMediaPlayer)
	{
		ui->horizontalSliderPlayer->setValue(0);

		ui->labelStartTime->setText("--:--");
		ui->labelEndTime->setText("--:--");
	}
}

void VLCPlayer::onVlcMediaPlayerStopped()
{
	if (m_vlcMediaPlayer)
	{
		ui->horizontalSliderPlayer->setValue(0);

		ui->labelStartTime->setText("--:--");
		ui->labelEndTime->setText("--:--");
	}
}

void VLCPlayer::on_btnOpen_clicked()
{
	//释放之前播放器
	if (m_vlcMediaPlayer) {
		if (libvlc_media_player_is_playing(m_vlcMediaPlayer)){
			libvlc_media_player_stop(m_vlcMediaPlayer);
		}

		libvlc_media_player_release(m_vlcMediaPlayer);
		m_vlcMediaPlayer = nullptr;
	}

	if (m_vlcMedia) {
		libvlc_media_release(m_vlcMedia);
		m_vlcMedia = nullptr;
	}
	
	//选择播放文件
	QString fileName = QFileDialog::getOpenFileName(this, "选择打开的文件", "", "*.*");
	if (fileName.isEmpty()) {
		return;
	}

	fileName = QDir::toNativeSeparators(fileName);

	//创建播放器
	m_vlcMedia = libvlc_media_new_path(m_vlcBase, fileName.toUtf8().data());
	if (!m_vlcMedia) {
		return;
	}

	m_vlcMediaPlayer = libvlc_media_player_new_from_media(m_vlcMedia);
	if (!m_vlcMediaPlayer) {
		libvlc_media_release(m_vlcMedia);
		m_vlcMedia = nullptr;
		return;
	}

	libvlc_media_parse(m_vlcMedia);
	libvlc_event_manager_t* em = libvlc_media_player_event_manager(m_vlcMediaPlayer);
	libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, &VLCPlayer::vlcEvents, this);
	libvlc_event_attach(em, libvlc_MediaPlayerEndReached, &VLCPlayer::vlcEvents, this);
	libvlc_event_attach(em, libvlc_MediaPlayerStopped, &VLCPlayer::vlcEvents, this);
	libvlc_event_attach(em, libvlc_MediaPlayerPlaying, &VLCPlayer::vlcEvents, this);
	libvlc_event_attach(em, libvlc_MediaPlayerPaused, &VLCPlayer::vlcEvents, this);

	libvlc_media_player_set_hwnd(m_vlcMediaPlayer, (void*)ui->widgetRender->winId());

	//禁用VLC鼠标、键盘事件
	libvlc_video_set_mouse_input(m_vlcMediaPlayer, 0);
	libvlc_video_set_key_input(m_vlcMediaPlayer, 0);
}

void VLCPlayer::on_btnPlay_clicked()
{
	if (m_vlcMediaPlayer) {
		if (libvlc_media_player_is_playing(m_vlcMediaPlayer)) {
			libvlc_media_player_pause(m_vlcMediaPlayer);
			ui->btnPlay->setText("播放");
		}else{
			setVideoAspectRatio(false);
			libvlc_media_player_play(m_vlcMediaPlayer);
			ui->btnPlay->setText("暂停");
		}
	}
}

void VLCPlayer::on_btnStop_clicked()
{
	if (m_vlcMediaPlayer) {
		libvlc_media_player_stop(m_vlcMediaPlayer);
	}

	ui->btnPlay->setText("播放");
}

void VLCPlayer::on_horizontalSliderPlayer_valueChanged(int value)
{
	if (m_vlcMediaPlayer)
	{
		libvlc_time_t time = value * 1000.0;
		libvlc_media_player_set_time(m_vlcMediaPlayer, time);
	}
}

void VLCPlayer::on_comboBoxRate_currentTextChanged(const QString& text)
{
	float rate = 1.0;
	if (sscanf(text.toStdString().c_str(), "%fx", &rate) < 0) {
		return;
	}

	if (m_vlcMediaPlayer)
	{
		libvlc_media_player_set_rate(m_vlcMediaPlayer, rate);
	}
}

void VLCPlayer::resizeEvent(QResizeEvent* event)
{
	setVideoAspectRatio(false);

	QWidget::resizeEvent(event);
}

void VLCPlayer::mouseDoubleClickEvent(QMouseEvent* event)
{
	if (this->isFullScreen())
	{
		this->showNormal();
	}
	else
	{
		this->showFullScreen();
	}

	ui->widgetTools->setVisible(!this->isFullScreen());

	QWidget::mouseDoubleClickEvent(event);
}

void VLCPlayer::vlcEvents(const libvlc_event_t* ev, void* param)
{
	VLCPlayer* vlcPlayer = reinterpret_cast<VLCPlayer*>(param);
	if (!vlcPlayer) {
		return;
	}

	switch (ev->type)
	{
	case libvlc_MediaPlayerTimeChanged:
		QMetaObject::invokeMethod(vlcPlayer, "onVlcMediaPlayerTimeChanged", Qt::QueuedConnection);
		break;

	case libvlc_MediaPlayerEndReached:
		QMetaObject::invokeMethod(vlcPlayer, "onVlcMediaPlayerEndReached", Qt::QueuedConnection);
		break;

	case libvlc_MediaPlayerStopped:
		QMetaObject::invokeMethod(vlcPlayer, "onVlcMediaPlayerStopped", Qt::QueuedConnection);
		break;

	case libvlc_MediaPlayerPlaying:
		QMetaObject::invokeMethod(vlcPlayer, "onVlcMediaPlayerPlaying", Qt::QueuedConnection);
		break;

	case libvlc_MediaPlayerPaused:
		break;

	default:
		break;
	}
}

void VLCPlayer::setVideoAspectRatio(bool aspectRatio)
{
	if (m_vlcMediaPlayer)
	{
		QString ratio = QString("%1:%2").arg(ui->widgetRender->width()).arg(ui->widgetRender->height());
		libvlc_video_set_aspect_ratio(m_vlcMediaPlayer, aspectRatio ? NULL : ratio.toUtf8().data());
	}
}

备注

  • VLC默认接管鼠标键盘事件,禁用后才能在QWidget中获取鼠标、键盘事件
	//禁用VLC鼠标、键盘事件
	libvlc_video_set_mouse_input(m_vlcMediaPlayer, 0);
	libvlc_video_set_key_input(m_vlcMediaPlayer, 0);
  • 源码下载地址

qt+libvlc简单播放器资源-CSDN文库

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QT 可以通过使用 Libvlc 库来实现视频播放功能。下面是使用 LibvlcQT 中播放视频的步骤: 1. 下载并安装 Libvlc 库。 2. 在 QT 项目中添加 Libvlc 库的头文件和库文件。可以在项目文件中添加以下代码: ```c++ // 添加头文件路径 INCLUDEPATH += /path/to/libvlc/include // 添加库文件路径和链接库 LIBS += -L/path/to/libvlc/lib \ -lvlc ``` 3. 在 QT 界面中添加一个 QFrame 控件作为视频播放区域。 4. 在 QT 中创建一个 VLCPlayer 类,用于加载和播放视频。可以参考以下代码: ```c++ #include <vlc/vlc.h> #include <QWidget> class VLCPlayer : public QWidget { Q_OBJECT public: explicit VLCPlayer(QWidget *parent = nullptr); ~VLCPlayer(); void play(const char *filePath); private: libvlc_instance_t *vlcInstance = nullptr; libvlc_media_player_t *mediaPlayer = nullptr; libvlc_media_t *media = nullptr; uintptr_t winId = 0; }; ``` 5. 在 VLCPlayer 类的实现文件中实现 play() 函数,用于加载和播放视频。可以参考以下代码: ```c++ #include "vlcplayer.h" #include <QX11Info> VLCPlayer::VLCPlayer(QWidget *parent) : QWidget(parent) { // 初始化 VLC 实例 vlcInstance = libvlc_new(0, nullptr); // 创建媒体播放器 mediaPlayer = libvlc_media_player_new(vlcInstance); // 获取视频播放区域的窗口句柄 winId = QX11Info::display() == nullptr ? winId() : QX11Info::display(); // 设置视频播放区域 libvlc_media_player_set_xwindow(mediaPlayer, winId); } VLCPlayer::~VLCPlayer() { // 释放资源 libvlc_media_player_release(mediaPlayer); libvlc_release(vlcInstance); } void VLCPlayer::play(const char *filePath) { // 创建媒体 media = libvlc_media_new_path(vlcInstance, filePath); // 设置媒体播放器的媒体 libvlc_media_player_set_media(mediaPlayer, media); // 播放视频 libvlc_media_player_play(mediaPlayer); } ``` 6. 在 QT 界面中调用 VLCPlayer 类的 play() 函数,传入视频文件路径即可播放视频。可以参考以下代码: ```c++ VLCPlayer *player = new VLCPlayer(ui->videoFrame); player->play("/path/to/video.mp4"); ``` 以上就是使用 LibvlcQT 中播放视频的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值