1. 程式人生 > >Qt呼叫VLC寫的視訊播放器原始碼

Qt呼叫VLC寫的視訊播放器原始碼

最近因為工作需要,做了個視訊播放器,能播放目前絕大多數格式,也能播放流媒體檔案,介面是用Qt做的,這個專案能在mac linux windows等平臺編譯執行,採用的是C++語言和C++的GUI庫Qt呼叫開源專案VLC提供出來的SDK,VLC是一個開源的視訊播放器,比較牛,沒聽過的朋友可以百度一下。
先上效果圖:由於是工作上的專案,只能把之前研究VLC的程式碼開源出來,後面修改的商業專案就不共享了。
 

下載地址:(下載後開啟vlc-qt\demo\demo-player\demo-player.pro即可編譯,不過需要4.8.4版本的QtSdk)
千腦下載:vlc-qt.rar
百度網盤我已委託@夜影幫忙上傳,待會不上!
百度網盤:
http://pan.baidu.com/share/link?shareid=446507&uk=1714263552


部分程式碼:

#include <QtGui/QFileDialog>
#include <QtGui/QInputDialog>

#include <vlc-qt/Common.h>
#include <vlc-qt/Instance.h>
#include <vlc-qt/Media.h>
#include <vlc-qt/MediaPlayer.h>

#include "DemoPlayer.h"
#include "ui_DemoPlayer.h"

DemoPlayer::DemoPlayer(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::DemoPlayer),
      _media(0)
{
    ui->setupUi(this);

    _instance = new VlcInstance(VlcCommon::args(), this);
    _player = new VlcMediaPlayer(_instance);
    _player->setVideoWidget(ui->video);

    ui->video->setMediaPlayer(_player);
    ui->volume->setMediaPlayer(_player);
    ui->volume->setVolume(100);
    ui->seek->setMediaPlayer(_player);

    connect(ui->actionOpenLocal, SIGNAL(triggered()), this, SLOT(openLocal()));
    connect(ui->actionOpenUrl, SIGNAL(triggered()), this, SLOT(openUrl()));
    connect(ui->actionPause, SIGNAL(triggered()), _player, SLOT(pause()));
    connect(ui->actionStop, SIGNAL(triggered()), _player, SLOT(stop()));
    connect(ui->pause, SIGNAL(clicked()), _player, SLOT(pause()));
    connect(ui->stop, SIGNAL(clicked()), _player, SLOT(stop()));
}

DemoPlayer::~DemoPlayer()
{
    delete _player;
    delete _media;
    delete _instance;
    delete ui;
}

void DemoPlayer::openLocal()
{
    QString file =
            QFileDialog::getOpenFileName(this, tr("Open file"),
                                         QDir::homePath(),
                                         tr("Multimedia files(*)"));

    if (file.isEmpty())
        return;

    _media = new VlcMedia(file, true, _instance);

    _player->open(_media);
}

void DemoPlayer::openUrl()
{
    QString url =
            QInputDialog::getText(this, tr("Open Url"), tr("Enter the URL you want to play"));

    if (url.isEmpty())
        return;

    _media = new VlcMedia(url, _instance);

    _player->open(_media);
}