基于vlc-Qt的视频播放器(支持添加视频列表单曲循环等)

Posted b11252

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于vlc-Qt的视频播放器(支持添加视频列表单曲循环等)相关的知识,希望对你有一定的参考价值。

        基于libvlc和Qt实现了一个视频播放器,可实现列表循环播放,单曲播放等,效果好于Qt自带的视频播放库。网上已有诸多基于vlc库的视频播放器,但设计列表播放的资源较少,基于vlc实现列表播放主要利用了libvlc_media_list_player_t和libvlc_media_list_t两个类。

也可查看本人github项目,附链接:

github主页:https://github.com/qin11152

环境准备及接口实现

        首先需要下载vlc相关库文件,可以在http://download.videolan.org/pub/videolan/vlc/下载对应的版本,安装后可在安装目录下找到对应的vlc库文件。在项目的包含目录中包含头文件,并链接libvlc和libvlccore两个动态库。之后便可进行相应开发。

相关编码实现

libvlc_instance_t* m_ptrVlcInstance = libvlc_new(0, NULL);

    使用vlc库首先需要new一个libvlc的实例,类型为libvlc_instance_t,后续相关操作均基于此对象。

播放器实例

libvlc_media_player_t* m_ptrMediaPlayer= libvlc_media_player_new(m_ptrVlcInstance);    //1媒体播放器
libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void *)ui.playWidget->winId());    //2设置播放窗口
libvlc_media_player_play():    //3播放
libvlc_media_player_pause():    //4暂停
libvlc_media_player_stop();    //5停止

        libvlc_media_player_t代表一个vlc的媒体播放器,可用于播放视频,但其功能不仅用于播放视频。上述代码中的2则是指定了媒体播放的界面,通过传递句柄可以指定媒体在具体的某一个widget上进行播放。后续的几个接口可以进行播放,暂停和结束,但因本项目为列表播放,所以不使用上述控制接口,若进行单视频播放可使用上述接口。

媒体列表及媒体列表播放器

libvlc_media_list_t* m_ptrMediaList=libvlc_media_list_new(m_ptrVlcInstance);    //1媒体列表
libvlc_media_list_player_t* m_ptrListMediaPlayer = libvlc_media_list_player_new(m_ptrVlcInstance);    //2媒体列表播放器
libvlc_media_list_player_set_media_list(m_ptrListMediaPlayer, m_ptrMediaList);    //3为列表播放器设置媒体列表
libvlc_media_list_player_set_media_player(m_ptrListMediaPlayer, m_ptrMediaPlayer);    //4为列表播放器设置播放器实例
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat);    //5循环模式

    libvlc_media_list_t(媒体列表)和libvlc_media_list_player_t(媒体列表播放器)两个对象是实现列表播放的重点。实例化两个对象后首先需要将媒体列表设定到媒体列表播放器中,如上述代码3所示。之后需要为媒体列表播放器设定播放器实体,如上述代码4所示,此播放器实体就是前面实例化的libvlc_media_player_t。可通过代码5设定列表播放器的循环方式(列表循环,单曲循环)。

libvlc_media_t* m_ptrMedia =libvlc_media_new_path(m_ptrVlcInstance,item.toUtf8().data());    //1实例化一个媒体对象
libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia);    //2添加媒体对象到媒体列表
libvlc_media_parse(m_ptrMedia);    //3释放资源 

    像媒体列表中传入媒体对象时,首先需要实例化一个媒体对象,实例化时需指定媒体所在的路径,然后调用上述代码中第2行接口将媒体对象塞到已有的媒体列表中即可。执行完后需要释放媒体对象。存在多个媒体时只需要循环的添加即可。媒体列表和媒体列表播放器也提供了诸多借口来进行控制,如下面代码所示,具体功能可参考注释。

libvlc_media_list_count( libvlc_media_list_t *p_ml );    //返回媒体列表中数量
libvlc_media_list_remove_index( libvlc_media_list_t *p_ml, int i_pos );    //根据index移除媒体列表中某一项
libvlc_media_list_insert_media( libvlc_media_list_t *p_ml, libvlc_media_t *p_md, int i_pos );    //像媒体列表中指定位置插入播放对象
libvlc_media_list_player_play(libvlc_media_list_player_t * p_mlp);    //列表播放开始
libvlc_media_list_player_pause(libvlc_media_list_player_t * p_mlp);    //列表播放暂停
libvlc_media_list_player_play_item_at_index(libvlc_media_list_player_t * p_mlp,int i_index);    //播放指定位置视频
libvlc_media_list_player_previous(libvlc_media_list_player_t * p_mlp);    //播放前一个视频
libvlc_media_list_player_next(libvlc_media_list_player_t * p_mlp);    //播放下一个视频

回调事件管理

libvlc_event_manager_t* em = libvlc_media_player_event_manager(m_ptrMediaPlayer);    //回到事件管理器
libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);    //注册关注的回调事件


void vlcEvents(const libvlc_event_t *ev, void *param)


 Q_UNUSED(param);
    switch (ev->type) 
    
    case libvlc_MediaPlayerTimeChanged:
    
        //从回调事件中得到播放时间
        auto tmpTime= ev->u.media_player_time_changed.new_time;
        QString time = "";
        m_ptrThis->transferTime(time, tmpTime);
        m_ptrThis->ui.horizontalSlider->setValue(tmpTime);
        m_ptrThis->ui.curTimeLabel->setText(time);
    
        break;
default:
        break;
    

    在使用vlc库的过程中,可以通过注册相关回调。使用方法如上述代码前半部分所示,修改libvlc_event_attach中第二个参数值即可得到不同事件的回调。对于回调事件的处理则需要重写vlcEvents函数实现,如上述代码中后半部分所示,对于播放时间、播放进度、播放视频切换至下一个等事件均需这样获取,然后才可以进行下一步处理。

其它接口

libvlc_media_get_duration( libvlc_media_t *p_md );    //获取视频时长
libvlc_media_player_set_time( libvlc_media_player_t *p_mi, libvlc_time_t i_time );    //设置视频播放至某一位置
libvlc_audio_get_volume( libvlc_media_player_t *p_mi );    //获取音量
libvlc_audio_set_volume( libvlc_media_player_t *p_mi, int i_volume );    //设置音量

    上述代码中列出了一些常用的接口,其作用如注释所示,如需要更详细介绍或其它接口,可查看文档或库文件。

实际工程

 代码如下,也可在本人github中查看下载具体工程。github地址:https://github.com/qin11152 

#include "MyMediaplayer.h"
#include <algorithm>

int MyMediaplayer::curTime =  0 ;
MyMediaplayer* MyMediaplayer::m_ptrThis =  nullptr ;
MyMediaplayer::MyMediaplayer(QWidget *parent)
    : QWidget(parent)

    ui.setupUi(this);
    initData();
    initConnect();
    m_ptrThis = this;


void MyMediaplayer::onPlayCurrentVedio()

    if (m_ptrMediaPlayer == nullptr)
    
        return;
    
    if (bool(libvlc_media_list_player_is_playing(m_ptrListMediaPlayer)))
    
        return;
    
    libvlc_media_list_player_play(m_ptrListMediaPlayer);
    int volumes = libvlc_audio_get_volume(m_ptrMediaPlayer);
    ui.VolumeSlider->setValue(volumes);


void MyMediaplayer::onPauseCurrentVideo()

    if (m_ptrListMediaPlayer == nullptr)
    
        return;
    
    if (bool(libvlc_media_list_player_is_playing(m_ptrListMediaPlayer)))
    
        m_bIsPlay = false;
        libvlc_media_list_player_pause(m_ptrListMediaPlayer);
    


void MyMediaplayer::onStopCurrentVideo()

    if (m_ptrListMediaPlayer == nullptr)
    
        return;
    
    m_bIsPlay = false;
    libvlc_media_list_player_stop(m_ptrListMediaPlayer);
    freeVlc();


void MyMediaplayer::onOpenPushButtonClicked()

    m_vecPlayList.clear();
    if (m_ptrVlcInstance)
    
        freeVlc();
    
    QStringList tmp = QFileDialog::getOpenFileNames(this, u8"打开视频", ".", u8"视频文件(*.mp4 *.mp3 *.flv *.3gp *.rmvb)");
    if (tmp.empty())
    
        return;
    
    for (auto& item : tmp)
    
        if (std::find(m_vecPlayList.begin(), m_vecPlayList.end(), item) == m_vecPlayList.end())
        
            m_vecPlayList.push_back(QDir::toNativeSeparators(item));
        
    
    m_strCurPath = QDir::toNativeSeparators(tmp[0]);
    setplayList();


void MyMediaplayer::onPausePushButtonClicked()

    onPauseCurrentVideo();


void MyMediaplayer::onPlayPushButtonClicked()

    onPlayCurrentVedio();


void MyMediaplayer::onStopPushButtonClicked()

    onStopCurrentVideo();


void MyMediaplayer::onSliderRelease(int val)

    int time = ui.horizontalSlider->value();
    libvlc_media_player_set_time(m_ptrMediaPlayer, val);


void MyMediaplayer::onSingleButtonClicked()

    if (m_bIsSingleCycle)
    
        ui.pushButton->setText(u8"列表循环");
        m_bIsSingleCycle = false;
        if (nullptr != m_ptrListMediaPlayer)
        
            libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_loop);
        
    
    else
    
        ui.pushButton->setText(u8"单曲循环");
        m_bIsSingleCycle = true;
        if (nullptr != m_ptrListMediaPlayer)
        
            libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat);
        
    


void MyMediaplayer::onSignalFreshButtonClicked()

    /*if (!m_ptrVlcInstance)
    
        return;
    
    QString item = u8"D:/技改技措业务_产品技术.mp4";
    item = QDir::toNativeSeparators(item);
    m_ptrMedia = libvlc_media_new_path(m_ptrVlcInstance, item.toUtf8().data());
    if (!m_ptrMedia)
    
        return;
    
    libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia);
    libvlc_media_parse(m_ptrMedia);
    libvlc_media_release(m_ptrMedia);*/
    return;


void MyMediaplayer::initData()

    ui.VolumeSlider->setMinimum(0);
    ui.VolumeSlider->setMaximum(100);
    m_ptrFileDialog = new QFileDialog(this);
    m_ptrFileDialog->hide();


bool MyMediaplayer::initVlcData()

    m_ptrVlcInstance = libvlc_new(0, NULL);
    if (!m_ptrVlcInstance)
    
        qDebug() << "qqqq create vlc failed";
        qDebug() << libvlc_errmsg;
        freeVlc();
        return false;
    
    m_ptrListMediaPlayer = libvlc_media_list_player_new(m_ptrVlcInstance);
    if (!m_ptrListMediaPlayer)
    
        qDebug() << "qqqq create m_ptrListMediaPlayer failed";
        freeVlc();
        return false;
    
    m_ptrMediaList = libvlc_media_list_new(m_ptrVlcInstance);
    if (!m_ptrMediaList)
    
        qDebug() << "qqqq create m_ptrMediaList failed";
        freeVlc();
        return false;
    
    m_ptrMediaPlayer = libvlc_media_player_new(m_ptrVlcInstance);
    libvlc_video_set_mouse_input(m_ptrMediaPlayer, false);
    libvlc_video_set_key_input(m_ptrMediaPlayer, false);
    if (!m_ptrMediaPlayer)
    
        freeVlc();
        qDebug() << "qqqq my media player failed";
        return false;
    
    libvlc_event_manager_t* em = libvlc_media_player_event_manager(m_ptrMediaPlayer);
    libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerPositionChanged, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerLengthChanged, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerTitleChanged, vlcEvents, this);
    libvlc_event_attach(em, libvlc_MediaPlayerVout, vlcEvents, this);
    //libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void *)ui.playWidget->winId());
    return true;


void MyMediaplayer::initConnect()

    connect(ui.openPushButton, &QPushButton::clicked, this, &MyMediaplayer::onOpenPushButtonClicked);
    connect(ui.playPushButton, &QPushButton::clicked, this, &MyMediaplayer::onPlayPushButtonClicked);
    connect(ui.pausePushButton, &QPushButton::clicked, this, &MyMediaplayer::onPausePushButtonClicked);
    connect(ui.stopPushButton, &QPushButton::clicked, this, &MyMediaplayer::onStopPushButtonClicked);
    connect(ui.horizontalSlider, &QSlider::sliderMoved, this, &MyMediaplayer::onSliderRelease);
    connect(ui.pushButton, &QPushButton::clicked, this, &MyMediaplayer::onSingleButtonClicked);
    connect(ui.VolumeSlider, &QSlider::valueChanged, this, [&](int val)
        
            if (nullptr != m_ptrMediaPlayer)
            
                qDebug() << "qqqq val:" << val;
                libvlc_audio_set_volume(m_ptrMediaPlayer, val);
            
        );


void MyMediaplayer::freeVlc()

    if (m_ptrMedia)
    
        libvlc_media_release(m_ptrMedia);
        m_ptrMedia = nullptr;
    
    if (m_ptrMediaPlayer)
    
        libvlc_media_player_stop(m_ptrMediaPlayer);
        libvlc_media_player_release(m_ptrMediaPlayer);
        m_ptrMediaPlayer = nullptr;
    
    if (m_ptrVlcInstance)
    
        libvlc_release(m_ptrVlcInstance);
        m_ptrVlcInstance = nullptr;
    
    if (m_ptrListMediaPlayer)
    
        libvlc_media_list_player_stop(m_ptrListMediaPlayer);
        libvlc_media_list_player_release(m_ptrListMediaPlayer);
        m_ptrListMediaPlayer = nullptr;
    
    if (m_ptrMediaList)
    
        libvlc_media_list_release(m_ptrMediaList);
        m_ptrMediaList = nullptr;
    


void MyMediaplayer::transferTime(QString& time, int msTime)

    msTime = msTime / 1000;
    int hour = msTime / 3600;
    msTime = msTime % 3600;
    int minute = msTime / 60;
    msTime = msTime % 60;
    if (hour > 0)
    
        time += QString::number(hour);
        time += ":";
    
    if (minute < 10)
    
        time += "0";
        time += QString::number(minute);
        time += ":";
    
    else
    
        time += QString::number(minute);
        time += ":";
    
    if (msTime < 10)
    
        time += "0";
        time += QString::number(msTime);
    
    else
    
        time += QString::number(msTime);
    


void MyMediaplayer::setplayList()

    if (m_ptrVlcInstance)
    
        freeVlc();
    
    if (!initVlcData())
    
        return;
    
    bool first = true;
    for (auto& item : m_vecPlayList)
    
        qDebug() << "qqqq path is" << item.toUtf8().data();
        m_ptrMedia = libvlc_media_new_path(m_ptrVlcInstance, item.toUtf8().data());
        if (m_ptrMedia)
        
            qDebug() << "qqqq create media succ";
        
        else
        
            qDebug() << "qqqq create media failed";
        
        libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia);
        libvlc_media_parse(m_ptrMedia);
        if (first)
        
            first = false;
            int time = libvlc_media_get_duration(m_ptrMedia);
            QString strTime = "";
            ui.horizontalSlider->setMaximum(time);
            ui.horizontalSlider->setMinimum(0);
            transferTime(strTime, time);
            ui.totalTimelabel->setText(strTime);
        
        libvlc_media_release(m_ptrMedia);
    
    m_ptrMedia = nullptr;
    libvlc_media_list_player_set_media_list(m_ptrListMediaPlayer, m_ptrMediaList);
    libvlc_media_list_player_set_media_player(m_ptrListMediaPlayer, m_ptrMediaPlayer);
    if (m_bIsSingleCycle)
    
        libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat);
    
    else
    
        libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_loop);
    
    libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void*)ui.playWidget->winId());


void MyMediaplayer::vlcEvents(const libvlc_event_t* ev, void* param)

    Q_UNUSED(param);
    switch (ev->type)
    
    case libvlc_MediaPlayerTimeChanged:
    
        //curTime = ev->u.media_player_time_changed.new_time;
        auto tmpTime = ev->u.media_player_time_changed.new_time;
        QString time = "";
        m_ptrThis->transferTime(time, tmpTime);
        m_ptrThis->ui.horizontalSlider->setValue(tmpTime);
        m_ptrThis->ui.curTimeLabel->setText(time);
    
    break;
    case libvlc_MediaPlayerEndReached:
        break;
    case libvlc_MediaPlayerStopped:
        //m_ptrThis->onPlayFinish();
        break;
    case libvlc_MediaPlayerPlaying:
        break;
    case libvlc_MediaPlayerPaused:
        break;
    case libvlc_MediaPlayerPositionChanged:
    
    
    break;
    case libvlc_MediaPlayerVout:
    
        qDebug() << ev->u.media_player_vout.new_count;
    
    break;
    case libvlc_MediaPlayerLengthChanged:
    
        int time = (ev->u.media_player_length_changed.new_length);
        qDebug() << "qqqq" << time;
        QString strtime = "";
        m_ptrThis->transferTime(strtime, time);
        m_ptrThis->ui.horizontalSlider->setMaximum(time);
        m_ptrThis->ui.totalTimelabel->setText(strtime);
    
    break;
    default:
        break;
    


MyMediaplayer::~MyMediaplayer()

    freeVlc();
    delete m_ptrFileDialog;
    m_ptrFileDialog = nullptr;

基于libVLC的视频播放器之十五:添加播放列表

一.效果

二.原理

在VLC-Qt中已经封装了播放列表相关的操作,比如说上一个、下一个、顺序播放、循环播放和重复播放。相关的两个类是VlcMediaList和VlcMediaListPlayer,
VlcMediaList持有多个VlcMedia媒体项,VlcMediaListPlayer通过setMediaList方法关联VlcMediaList,并在构造时传入VlcMediaPlayer对象,这样VlcMediaListPlayer内部就能通VlcMediaList和VlcMediaPlayer实现播放控制了。
一个典型的播放列表操作如下:

void TestMediaList::list()
{
    VlcMedia *referenceMedia = new VlcMedia("http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi", _instance);

    VlcMediaList *mediaList = new VlcMediaList(_instance);
    mediaList->addMedia(new VlcMedia("http://videos.hd-trailers.net/Hobbit_The_Battle_Of_The_Five_Armies_2014_Trailer2_5.1-480p-HDTN.mp4", _instance));
    mediaList->addMedia(referenceMedia);
    mediaList->insertMedia(new VlcMedia(QString(SAMPLES_DIR) &

以上是关于基于vlc-Qt的视频播放器(支持添加视频列表单曲循环等)的主要内容,如果未能解决你的问题,请参考以下文章

基于libVLC的视频播放器之十四:VLC-Qt全屏与退出全屏

视频播放器:QtCreator运行VLC-Qt官方示例代码

基于FFmpeg+rtsp读取摄像头实时图像

javascript html5视频播放器

javascript html5视频播放器

QT软件开发-基于FFMPEG设计视频播放器-支持流媒体地址播放