Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction to libvLC

VLC this player (as long as it involves software development) basic should have heard used, its powerful function will not say more. VLC media Player website: www.videolan.org/

Libvlc source and library files download address: FTP. Heanet. Ie/pub/videola…

This is where you can download all libvlc versions.

To develop your own video player, the first library that comes to mind is FFMPEG. If you want to get started immediately and quickly develop a player without understanding the audio and video basics, then calling libvLC directly is a great choice.

With window embedding, it takes very little code to develop a working video player. Libvlc also supports cross-platform, mainstream platforms officially provide library files, also provide source code, embedded platforms can cross-compile themselves.

The following will introduce the use of QT as the UI interface, call libvLC to complete a simple video player development, using window embedded way. The VLC version used is the latest version at the time of publication.

Two, implementation code

2.1 the widget. The CPP

#include "widget.h"
#include "ui_widget.h"

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

    // Create and initialize the libvlc instance
    vlc_base=libvlc_new(0.nullptr);

    this->setWindowTitle("Video player designed by LibvLC");
}


Widget::~Widget()
{
    delete ui;
}

// Select the video
void Widget::on_pushButton_open_clicked(a)
{
    /* Select file */
    QString filename=QFileDialog::getOpenFileName(this."Select open file"."D:/".tr("*. *"));

    std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\ \'));
    qDebug() < <"Broadcast media :"<<filename;

    /* Create media for a specific file path */
    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 execution error.";
        return;
    }

    /* Creates a player object based on the given media object */
    if(vlc_mediaPlayer)libvlc_media_player_release(vlc_mediaPlayer);
    vlc_mediaPlayer=libvlc_media_player_new_from_media(vlc_media);

    /* Set win32/ Win64 window handle to give media player media output */
    libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui->widget->winId());

    /* Play the media file */
    if(vlc_mediaPlayer)libvlc_media_player_play(vlc_mediaPlayer);
}


// Pause and continue
void Widget::on_pushButton_pause_clicked(a)
{
    if(vlc_mediaPlayer)libvlc_media_player_pause(vlc_mediaPlayer);
}

/ / stop
void Widget::on_pushButton_stop_clicked(a)
{
    if(vlc_mediaPlayer)libvlc_media_player_stop(vlc_mediaPlayer);
}

Copy the code

2.2 widget. H

#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(a);private slots:
    void on_pushButton_open_clicked(a);

    void on_pushButton_pause_clicked(a);

    void on_pushButton_stop_clicked(a);
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
Copy the code

2.3 Pro engineering documents

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.0INCLUDEPATH += $$PWD\VLC\ SDK \include #VLC header directory INCLUDEPATH += $$PWD\VLC\ SDK \include\ VLC\ plugins #VLC header directory optional INCLUDEPATH +=$$PWD\VLC\ SDK \include\ VLC #VLC header contains directory optional LIBS +=$$PWD\VLC\ SDK \lib\libvlc.lib #VLC library file path LIBS +=$$PWD\VLC\ SDK \lib\libvlccore. Lib # Optional SOURCES += \ main.cpp \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui # Default rulesfor deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:! android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
Copy the code