用QT制作本地视频播放器

用QT制作本地视频播放器

先创建一个Qwidget工程
用QT制作本地视频播放器_第1张图片
在工程里第一行加上QT += core gui multimedia multimediawidgets sql

widget.H文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 



class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void closeEvent(QCloseEvent *event);

    void List_file();
    void Play();
    void Pause();
    void Del_file();
    void Move(qint64 position);
    void seekChange(int position);

private:
    QListWidget *list;
    QMediaPlayer *player;
    QVideoWidget *video;
    QSplitter *splitter;
    QSlider *Slider;
    QSqlDatabase db;
    QString name_file;
    QString dirname;
    QLabel *label;



    QPushButton *bt1;
    QPushButton *bt2;
    QPushButton *bt3;
    QPushButton *bt4;
    QPushButton *bt5;


    int flag=0;


};

#endif // WIDGET_H

widget.cpp代码(main.cpp文件不用修改

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("爱奇艺");
    this->setWindowIcon(QIcon(":/ico/aqy.bmp"));
    this->resize(QSize(700,500));   //设置Widget窗口初始大小

    //创建数据库
    db = QSqlDatabase::addDatabase("QSQLITE");     //添加sqlite驱动
    db.setDatabaseName("my.db");

    db.open();
    QString mlist = "create table mlist(filename text,path text);";
    db.exec(mlist);

    //添加水平布局器和垂直布局器
    QHBoxLayout *hbox_1 = new QHBoxLayout;
    QHBoxLayout *hbox_2 = new QHBoxLayout;
    QVBoxLayout *vbox   = new QVBoxLayout;


    //添加控件
    list   = new QListWidget(this);
    Slider = new QSlider(Qt::Horizontal);
    label  = new QLabel(this);



    video = new QVideoWidget(this);
    //video->setMinimumSize(400,300);
    list->setMaximumWidth(200);

    this->setAutoFillBackground(true); //控制控件背景是否填充
    video->setAutoFillBackground(true);

    QPalette palette_1;
    QPalette palette_2;
    palette_1.setColor(QPalette::Background,QColor(0,0,0));
    //palette_1.setBrush(QPalette::Background,QBrush(QPixmap(":/ico/13.jpg")));
    palette_2.setBrush(QPalette::Background,QBrush(QPixmap(":/ico/aqi1.bmp")));
    video->setPalette(palette_2);
    this->setPalette(palette_1);


    player = new QMediaPlayer(this);



    //设置图片按钮
    QIcon ico1(":/ico/china.png");
    bt1 = new QPushButton(this);
    bt1->setIcon(ico1);
    bt1->setIconSize(QSize(50,50));
    bt1->setFlat(true);             //去掉按钮边框
    bt1->setToolTip("添加文件");     //按钮标签

    QIcon ico2(":/ico/zant.png");
    bt2 = new QPushButton(this);
    bt2->setIcon(ico2);
    bt2->setIconSize(QSize(60,60));
    bt2->setFlat(true);
    bt2->setToolTip("暂停");

    QIcon ico3(":/ico/bo.png");
    bt3 = new QPushButton(this);
    bt3->setIcon(ico3);
    bt3->setIconSize(QSize(80,80));
    bt3->setFlat(true);             //去掉按钮边框
    bt3->setToolTip("播放");     //按钮标签

    QIcon ico4(":/ico/Trash.png");
    bt4 = new QPushButton(this);
    bt4->setIcon(ico4);
    bt4->setIconSize(QSize(50,50));
    bt4->setFlat(true);
    bt4->setToolTip("删除文件");

    hbox_1->addWidget(list);
    hbox_1->addWidget(video);

    hbox_2->addWidget(bt1);     //把按钮放入布局器
    hbox_2->addWidget(bt2);
    hbox_2->addWidget(bt3);
    hbox_2->addWidget(bt4);
    hbox_2->addWidget(Slider);
    hbox_2->addWidget(label);


    vbox->addLayout(hbox_1);
    vbox->addLayout(hbox_2);

    this->setLayout(vbox);      //把布局器贴到界面
    Slider = new QSlider(Qt::Horizontal);//把进度条贴到界面

    connect(bt1,SIGNAL(clicked(bool)),this,SLOT(List_file()));
    connect(bt2,SIGNAL(clicked(bool)),this,SLOT(Pause()));
    connect(bt3,SIGNAL(clicked(bool)),this,SLOT(Play()));
    connect(bt4,SIGNAL(clicked(bool)),this,SLOT(Del_file()));
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(updatePosition(qint64)));
    connect(Slider,SIGNAL(sliderMoved(int)),this,SLOT(seekchange(int)));


    //播放列表
    QString sql = "select * from mlist;";
    QSqlQuery query;
    if( query.exec(sql)){
        qDebug()<<"显示列表";
    }
    else{
        qDebug()<<"显示失败";
    }
    while( query.next() ){
        QString file_name =QString( query.value("filename").toString() );       //转换类型
        list->addItem(file_name);
    }
}

void Widget::List_file()
{
    QString dirname = QFileDialog::getExistingDirectory();
    //QString dirname = QFileDialog::getOpenFileName();       //打开文件对话框,获取打开的文件名
    QDir dir(dirname);  //定义QDir对象

    QStringList   filter; //设置过滤器
    filter <<"*.mp3"<<"*.mp4"<<"*.flv"<<"*.avi";
/*
    QList  lst;
    lst =  dir.entryList(filter);   //获取文件夹下的所有文件名
    for(int i=0;iaddItem(lst.at(i));
        qDebug()<
    QList <QFileInfo> lst;
    lst = dir.entryInfoList(filter);

    for(int i=0;i<lst.size();i++){
        list->addItem(lst.at(i).fileName());
        QString name = lst.at(i).fileName();
        QString path = lst.at(i).absoluteFilePath();
        QString sql = QString("insert into mlist values('%1','%2');").arg(name).arg(path);
        qDebug()<<sql;

        QSqlQuery query;

           if( query.exec(sql)){

               qDebug()<<"插入数据库成功";
           }
           else{
               qDebug()<<"插入数据库失败";
           }


        //qDebug()<
    }
}


void Widget::Play()
{
    QString filename;
    //选择文件播放
    QListWidgetItem* item = list->currentItem();  //获取当前item
    name_file = item->text();

    QString sql = QString("select * from mlist where filename='%1';").arg(name_file);
    QSqlQuery query;
    if(query.exec(sql)){
        qDebug()<<"查询成功";
    }
    else {
        qDebug()<<"查询失败";
    }
    if( query.next()){

        qDebug()<<"登录成功";
        filename = query.value("path").toString();
    }
    else{
        qDebug()<<"登录失败";
    }


    player->setMedia(QUrl::fromLocalFile(filename));

    qDebug()<<"name:"<<filename;

    //显示视频
    player->setVideoOutput(video);
    video->show();
    player->play();
  /*  if(flag==1){
        player->pause();
        flag=0;
    }
    else if(flag==0){
        player->play();
        flag=1;
    }
    //player->play();
    //qDebug()<text();
*/
}

void Widget::Del_file()
{
    QListWidgetItem* item = list->currentItem();  //获取当前item
    name_file = item->text();

    QString sql = QString("delete from mlist where filename='%1';").arg(name_file);
    QSqlQuery query;
    if(query.exec(sql)){
        qDebug()<<"删除成功";
    }
    else {
        qDebug()<<"删除失败";
    }
    delete item;
}

void Widget::Pause()
{
    //停止
 /*   connect(bt2, SIGNAL(clicked()), video, SLOT(stop()));

    connect(bt2, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));

    bt2->setEnabled(false);
*/
    if(player->state()==QMediaPlayer::PlayingState)
    {
        bt2->setToolTip("暂停");
        player->pause();
    }
    else
    {
        bt2->setToolTip("开始");
        player->play();
    }
}

    //窗口关闭弹窗
void Widget::closeEvent(QCloseEvent *event)
{
    //event->ignore();
    if(QMessageBox::question(this,tr("Quit"),tr("你确定要退出这个应用吗?"),QMessageBox::Yes,QMessageBox::No) == QMessageBox::Yes){
        event->accept();
    }
    else {
        event->ignore();
    }
}

void Widget::Move(qint64 position)
{
    this->Slider->setMaximum(player->duration()/1000);
    this->Slider->setValue(position/1000);
    qint64 moved = position;
    QTime movetime(0,(moved/60000)%60,(moved/1000)%60);
    this->label->setText(movetime.toString("mm:ss"));
}

void Widget::seekChange(int position)  //---------拖动进度条
{
    player->setPosition(position*1000);
    int moved = position*1000;
    QTime movetime(0,(moved/60000)%60,(moved/1000)%60);
    this->label->setText(movetime.toString("mm:ss"));
}

Widget::~Widget()
{

}

运行成功后的界面用QT制作本地视频播放器_第2张图片

你可能感兴趣的:(用QT制作本地视频播放器)