码迷,mamicode.com
首页 > 其他好文 > 详细

qt实现一个简单的播放器

时间:2016-10-29 14:23:58      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:signal   代码   slot   setvalue   ref   reads   sig   next   max   

ui界面设计如下:

技术分享

2、代码结构

技术分享

3、dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include<QStringList>
#include<QDebug>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->lb_dis->installEventFilter(this);
    pro = new QProcess();
    tp = new QTimer();
    tp1 = new QTimer();
    flag =0;
    flag1 =0;
    music = "libai.avi";
    i=1;
    fullflag = 0;
    musiclist[0]= "libai.avi";
    musiclist[1] = "kjwcbz.avi";
   // connect(pro,SIGNAL(readyReadStandardOutput()),this,SLOT(gettime()));
    connect(tp,SIGNAL(timeout()),this,SLOT(gettime()));
    connect(tp1,SIGNAL(timeout()),this,SLOT(changimage()));
    connect(pro,SIGNAL(finished(int)),this,SLOT(getmusic()));
  //  connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(setlineeditvalue(int)));
    tp1->start(2000);
}

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

void Dialog::on_bt_play_clicked()  //click play
{
    QStringList cmd_list;
    //pro->write("quit\n");
    tp->start(100);
    cmd_list<<"/mnt/hgfs/shared/qt/qmyplay/player/"+music;
    cmd_list<<"-wid";
    cmd_list<< QString::number(ui->lb_dis->winId());
    cmd_list<<"-slave";
    cmd_list<<"-quiet";

    pro->start("mplayer",cmd_list);


}

void Dialog::on_bt_pause_clicked()  //click pause
{
    switch (flag) {
    case 1:
    {
        pro->write("pause\n");
            tp->start(100);
            flag=0;
            break;
    }
    case 0:
    {
pro->write("pause\n");
    tp->stop();
    flag++;
   break;
    }

    //qDebug()<<"pause";
}
}
void Dialog::gettime() //get current and time length
{
    // pro->write("pause\n");
     pro->write("get_time_pos\n");
     pro->write("get_time_length\n");

while(pro->canReadLine())
{
    QString timeinfo = pro->readLine();
    QString timeend = pro->readLine();
   // qDebug()<<timeinfo;
    //qDebug()<<timeend;
    s = timeinfo.section(".",-2,-2);
    s1 = timeend.section(".",-2,-2);
    s = s.section("=",-1,-1);
    s1 = s1.section("=",-1,-1);
  //  qDebug()<<"info"<<s;
  //  qDebug()<<"end"<<s1;
  //  qDebug()<<s;
    int a1,a2,b1,b2;
    a1 = s.toInt()%60;//geiwei
    a2 = s.toInt()/60;//shiwei
    QString ss,ss1,ss2,ss3;
    ss.setNum(a1);
    ss1.setNum(a2);
    b1 = s1.toInt()%60;//geiwei
    b2 = s1.toInt()/60;//shiwei
    ss2.setNum(b1);
    ss3.setNum(b2);
   // ui->horizontalSlider->setMinimumSize(0);
    ui->horizontalSlider->setMaximum(s1.toInt());
    ui->horizontalSlider->setValue(s.toInt());
    ui->lb_current_time_2->setText("00:"+ss3+":"+ss2);
    ui->lb_current_time->setText("00:"+ss1+":"+ss);
}
}

void Dialog::on_bt_input_clicked()  //input music
{
    QDir dir("/mnt/hgfs/shared/qt/qmyplay/player");
    QStringList filelist;
    filelist<<"*.avi"<<"*.mp3";
    QStringList filename = dir.entryList(filelist);
    //if(ui->listWidget->currentItem()==NULL)
    ui->listWidget->addItems(filename);

}

void Dialog::on_bt_input_2_clicked() //delete music
{
    delete ui->listWidget->currentItem();
}

void Dialog::on_bt_speed_clicked()//speed ++
{
    pro->write("seek +5 ");
}

void Dialog::on_bt_speed_2_clicked()//speed --
{
    pro->write("seek -5");
}
void Dialog::sendmsg()
{


}

void Dialog::on_horizontalSlider_sliderMoved(int position)//position
{
    tp->stop();
    pro->write("pause\n");
//ui->horizontalSlider->setValue(position);
QString tmp;

   tmp.setNum(position);
   s=tmp;
  // qDebug()<<tmp;
   QString posi ="seek " +tmp+" 2";
   pro->write(posi.toLatin1());
   qDebug()<<posi.toLatin1();
   tp->start(100);
   pro->write("pause");
//qDebug()<<position<<"position";
}



/*void Dialog::on_listWidget_Clicked(const QModelIndex &index)
{
music = ui->listWidget->currentItem()->text();
pro->write("quit\n");
QStringList cmd_list;
 tp->start(100);
cmd_list<<"/mnt/hgfs/shared/qt/qmyplay/player/"+music;
cmd_list<<"-wid";
cmd_list<< QString::number(ui->lb_dis->winId());
cmd_list<<"-slave";
cmd_list<<"-quiet";
pro->start("mplayer",cmd_list);
}
*/
/*void Dialog::on_horizontalSlider_sliderPressed()
{
    QString tmp;

       tmp.setNum(ui->horizontalSlider->value());
       qDebug()<<tmp;
       QString posi ="seek" +tmp+" 2";
       pro->write(posi.toLatin1());
}*/

void Dialog::on_listWidget_clicked(const QModelIndex &index)
{
    music = ui->listWidget->currentItem()->text();
    pro->write("quit\n");
    on_bt_play_clicked();
    //pro->start();
    //pro->write("quit\n");

}

void Dialog::on_bt_speed_3_clicked()//volume ++
{
    pro->write("volume +5");
}

void Dialog::on_bt_speed_4_clicked()//volume --
{
    pro->write("volume -5");
}
void Dialog::changimage()  //change image
{

    //this->resize(715, 423);
    switch (flag1) {
    case 0:
         this->setStyleSheet(QStringLiteral("QDialog{border-image: url(:/new/prefix1/player/t01185f568f2f064f39.jpg);}"));
        flag1++;
        break;
    case 1:
        this->setStyleSheet(QStringLiteral("QDialog{border-image: url(:/new/prefix1/player/t01c952a08ad7577d58.jpg);}"));
        flag1++;
        break;
    case 2:
        this->setStyleSheet(QStringLiteral("QDialog{border-image: url(:/new/prefix1/player/t01f4b009b55efffa08.jpg);}"));
        flag1 = 0;
        break;

    }
}
void Dialog::setlineeditvalue(int value)  //set line value
{
    int pos = ui->horizontalSlider->value();
    QString tmp;

       tmp.setNum(pos);
      qDebug()<<tmp;
       QString posi ="seek" +tmp+" 2";
       pro->write(posi.toLatin1());
       //ui->horizontalSlider->setValue(pos);
        //qDebug()<<ui->horizontalSlider->value();
//toInt
}
void Dialog::getmusic()   //recycle play
{
    // pro->close();
    switch(i)
    {case 1:
        music = musiclist[1];
        i =0;
        break;
    case 0:
        music = musiclist[0];
        i++;
        break;
    }
    qDebug()<<"end";
    qDebug()<<music;

   // on_bt_play_clicked();
      on_bt_play_clicked();
        //pro->start();
    qDebug()<<"end";
}

void Dialog::on_bt_full_clicked()  //full screen button
{
    switch (fullflag) {
    case 0:
        //this->setWindowState(Qt::Dialog);
        this->setWindowFlags(Qt::Dialog);
        this->showFullScreen();
        fullflag++;
        break;
    case 1:
        //this->setWindowFlags(Qt::SubWindow);
        this->showNormal();
        fullflag=0;
        break;
    }

   // qDebug()<<"fi;;";
}

void Dialog::on_bt_prev_clicked()  //prev play
{
    if(music == "libai.avi")
    music = musiclist[1];
    pro->write("quit\n");
    on_bt_play_clicked();
}

void Dialog::on_bt_next_clicked()  //next play
{
    if(music == "libai.avi")
    music = musiclist[1];
    pro->write("quit\n");
    on_bt_play_clicked();
}
bool Dialog::eventFilter(QObject *obj, QEvent *evt)  //mouse double click and click
{
    if(obj==ui->lb_dis)
    {
        if(evt->type()==QEvent::MouseButtonDblClick)
        {
            switch (fullflag) {
            case 0:
                //this->setWindowState(Qt::Dialog);
                this->setWindowFlags(Qt::Dialog);
                this->showFullScreen();
                fullflag++;
                break;
            case 1:
                //this->setWindowFlags(Qt::SubWindow);
                this->showNormal();
                fullflag=0;
                break;
            }
        }
            if(evt->type() == QEvent::MouseButtonRelease)
            {
                switch (flag) {
                case 1:
                {
                    pro->write("pause\n");
                    tp->start(100);
                    flag=0;
                    break;
                }
                case 0:
                {
                    pro->write("pause\n");
                    tp->stop();
                    flag++;
                    break;
                }
            }
        }
    }
}

4、main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

dialog.h

 1 #ifndef DIALOG_H
 2 #define DIALOG_H
 3 
 4 #include <QDialog>
 5 #include<QStringList>
 6 #include<QProcess>
 7 #include <QScrollBar>
 8 #include<QAbstractSlider>
 9 #include<QTimer>
10 #include<QDir>
11 namespace Ui {
12 class Dialog;
13 }
14 
15 class Dialog : public QDialog
16 {
17     Q_OBJECT
18 
19 public:
20     explicit Dialog(QWidget *parent = 0);
21     ~Dialog();
22     bool eventFilter(QObject *obj, QEvent *evt);
23 
24 private slots:
25     void on_bt_play_clicked();
26     void gettime();
27     void on_bt_pause_clicked();
28     void sendmsg();
29     void on_bt_input_clicked();
30 
31     void on_bt_input_2_clicked();
32 
33     void on_bt_speed_clicked();
34 
35     void on_bt_speed_2_clicked();
36 
37     //void on_horizontalSlider_sliderMoved(int position);
38 
39     void on_horizontalSlider_sliderMoved(int position);
40     
41    //void on_horizontalSlider_valueChanged(int value);
42 
43   //  void on_listWidget_Clicked(const QModelIndex &index);
44 
45     //void on_horizontalSlider_sliderPressed();
46 
47     void on_listWidget_clicked(const QModelIndex &index);
48 
49     void on_bt_speed_3_clicked();
50     void getmusic();
51     void on_bt_speed_4_clicked();
52     void changimage();
53     void setlineeditvalue(int);
54     void on_bt_full_clicked();
55 
56     void on_bt_prev_clicked();
57 
58     void on_bt_next_clicked();
59 
60 private:
61     Ui::Dialog *ui;
62     QProcess *pro;
63     QAbstractSlider *asp;
64     QTimer *tp,*tp1;
65      QString s,s1,music;
66      int flag,flag1,i,fullflag;
67      QString musiclist[5];
68 
69 };
70 
71 #endif // DIALOG_H

 

qt实现一个简单的播放器

标签:signal   代码   slot   setvalue   ref   reads   sig   next   max   

原文地址:http://www.cnblogs.com/defen/p/6010617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!