QTplayer 程序

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include
#include

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    //void btnFileOpen_clicked();
    void on_btnFileOpen_clicked();

    void on_btnPlay_clicked();

    void on_btnPrevious_clicked();

    void on_btnNext_clicked();

    void on_dialVoice_valueChanged(int value);

    void on_horizontalSlider_valueChanged(int value);

    void doProcessTimeOut();

private:
    Ui::MainWindow *ui;

    QStringList playList;
    int playIndex;
    QProcess *playProcess;
    int isPlaying;
    QTimer *myTimer;

    void Init();
    void playMovie(const QString filename);
};
#endif // MAINWINDOW_H

------------------------------------------------------

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"

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

    Init();
    //connect(ui->btnFileOpen,&QPushButton::clicked,this,&MainWindow::btnFileOpen_clicked);
}

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

//slots
//void MainWindow::btnFileOpen_clicked()
//{
//    QStringList array = QFileDialog::getOpenFileNames(this,"add videos","/home/l/Videos","video(*.mp4)");
//    if (array.length() <=0) return;
//    playList = array;
//    playIndex = 0;
//    playMovie(playList.at(playIndex));
//}

void MainWindow::on_btnFileOpen_clicked()
{
    QStringList array = QFileDialog::getOpenFileNames(this,"add videos","/home/l/Videos","video(*.mp4)");
    if (array.length() <=0) return;
    playList = array;
    playIndex = 0;
    playMovie(playList.at(playIndex));
}

void MainWindow::on_btnPlay_clicked()
{
    QString cmd = "pause\n";
    switch (isPlaying)
    {
        case 0:
            break;
        case 1:
            isPlaying = 2;
            playProcess->write(cmd.toLatin1());
            ui->btnPlay->setIcon(QIcon(":/new/prefix1/image/icons-start.png"));
            break;
        case 2:
            isPlaying = 1;
            playProcess->write(cmd.toLatin1());
            ui->btnPlay->setIcon(QIcon(":/new/prefix1/image/stop.png"));
            break;
    }
}

void MainWindow::on_btnPrevious_clicked()
{
    playIndex--;
    if (playIndex < 0)
    {
        playIndex = playList.length() -1;
    }
    playMovie(playList.at(playIndex));
}

void MainWindow::on_btnNext_clicked()
{
    playIndex++;
    if (playIndex >= playList.length())
    {
        playIndex = 0;
    }
    playMovie(playList.at(playIndex));
}

void MainWindow::on_dialVoice_valueChanged(int value)
{
    if (isPlaying != 0)
    {
        QString cmd = QString("volume %1 1\n").arg(value);
        playProcess->write(cmd.toLatin1());
    }
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    if (isPlaying != 0)
    {
        QString cmd = QString("seek %1 1\n").arg(value);
        playProcess->write(cmd.toLatin1());
    }
}

void MainWindow::doProcessTimeOut()
{
    QString cmd = "get_percent_pos\n";
    playProcess->write(cmd.toLatin1());
}
//end slots

//function
void MainWindow::Init()
{
    playList.clear();
    playIndex=0;
    playProcess = new QProcess(this);
    isPlaying = 0;

    myTimer = new QTimer(this);
    connect(myTimer,&QTimer::timeout,this,&MainWindow::doProcessTimeOut);
}

void MainWindow::playMovie(const QString filename)
{
    QString program = "/usr/bin/mplayer";
    QStringList arguments;
    arguments << filename;
    arguments << "-slave";
    arguments << "-quiet";
    arguments << "-wid";
    arguments << QString::number(ui->label->winId());
    playProcess->close();
    playProcess->start(program,arguments);
    myTimer->start(1000*1);

    isPlaying = 1;
    ui->btnPlay->setIcon(QIcon(":/new/prefix1/image/stop.png"));

    QString cmd = QString("volume %1 1\n").arg(ui->dialVoice->value());
    playProcess->write(cmd.toLatin1());

    ui->horizontalSlider->setValue(0);
}
//end function

你可能感兴趣的:(QT,qt)