VLC这个播放器(只要涉及到软件开发)基本应该都听过用过,其功能的强大就不多说了。 VLC媒体播放器官网: https://www.videolan.org/
libvlc源码和库文件下载地址: http://ftp.heanet.ie/pub/videolan/vlc/
这里面汇总了所有libvlc版本的下载地址。
要自己开发视频播放器,首先想到的库就是ffmpeg,如果想立即上手,不想理解音视频底层,快速开发一个播放器,那么直接调用libvlc是一个非常好的选择。
如果采用窗口嵌入方式,只需要很少的代码就可以开发一个能正常使用的视频播放器了。并且libvlc也支持跨平台,主流的平台官方都提供了库文件,也提供了源码,嵌入式平台可以自己交叉编译。
下面就介绍使用QT作为UI界面,调用libvlc完成一个简单的视频播放器开发,采用窗口嵌入式方式。使用的VLC版本是截止发文时的最新版本。
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//创建并初始化libvlc实例
vlc_base=libvlc_new(0,nullptr);
this->setWindowTitle("libvlc设计的视频播放器");
}
Widget::~Widget()
{
delete ui;
}
//选择视频
void Widget::on_pushButton_open_clicked()
{
/*选择文件*/
QString filename=QFileDialog::getOpenFileName(this,"选择打开的文件","D:/",tr("*.*"));
std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\\'));
qDebug()<<"播放的媒体:"<<filename;
/*为特定文件路径创建媒体*/
if(vlc_media)libvlc_media_release(vlc_media);
vlc_media=libvlc_media_new_path(vlc_base,filename.toUtf8().data());
if(vlc_media==nullptr)
{
qDebug()<<"libvlc_media_new_path 执行错误.";
return;
}
/*根据给定的媒体对象创建一个播放器对象*/
if(vlc_mediaPlayer)libvlc_media_player_release(vlc_mediaPlayer);
vlc_mediaPlayer=libvlc_media_player_new_from_media(vlc_media);
/*设置给予媒体播放器媒体输出的win32/win64窗口句柄*/
libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui->widget->winId());
/*播放媒体文件*/
if(vlc_mediaPlayer)libvlc_media_player_play(vlc_mediaPlayer);
}
//暂停与继续
void Widget::on_pushButton_pause_clicked()
{
if(vlc_mediaPlayer)libvlc_media_player_pause(vlc_mediaPlayer);
}
//停止
void Widget::on_pushButton_stop_clicked()
{
if(vlc_mediaPlayer)libvlc_media_player_stop(vlc_mediaPlayer);
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <vlc/vlc.h>
#include <QDebug>
#include <QFileDialog>
#include <QMoveEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_open_clicked();
void on_pushButton_pause_clicked();
void on_pushButton_stop_clicked();
private:
Ui::Widget *ui;
libvlc_instance_t *vlc_base=nullptr;
libvlc_media_t *vlc_media=nullptr;
libvlc_media_player_t *vlc_mediaPlayer=nullptr;
Form *form;
};
#endif // WIDGET_H
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$PWD\VLC\sdk\include #VLC头文件包含目录
INCLUDEPATH += $$PWD\VLC\sdk\include\vlc\plugins #VLC头文件包含目录可选
INCLUDEPATH += $$PWD\VLC\sdk\include\vlc #VLC头文件包含目录 可选
LIBS +=$$PWD\VLC\sdk\lib\libvlc.lib #VLC库文件路径
LIBS +=$$PWD\VLC\sdk\lib\libvlccore.lib #可选
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target