视频播放器

视频播放app

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QWidget>
#include <QLabel>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QDropEvent>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = nullptr);
protected:
    void dragEnterEvent(QDragEnterEvent *e);
    void dropEvent(QDropEvent*);
    void mouseDoubleClickEvent(QMouseEvent *);

signals:
    void SignalSendPlayPath(const QString filename);
    void SignalSetFullShow(bool);
private:
    QString filename;//接收拖进来文件的路径
    bool isFull; //是否全屏播放
};
#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include <QDebug>
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    filename.clear();
    isFull = false;
    this->setAcceptDrops(true);//开启鼠标附属功能
    setStyleSheet("QLabel { background-color : black; }");
}

void MyLabel::dragEnterEvent(QDragEnterEvent *e){
    //获取拖拽文件的路径
    QString path = e->mimeData()->text();
    if(!filename.isEmpty()){
        return;
    } else {
        if(filename != path){//接收新的path值
            filename = path;
            e->accept(); // 继续传递事件
        }
    }
    qDebug() << "dragEnterEvent:" << filename;
}
// drag video into Label to play.
void MyLabel::dropEvent(QDropEvent *event){
    if(filename.isEmpty()){
        return;
    }
    QString path = this->filename;
    path.replace("file://", "");
    path.replace("\r\n", "");
    qDebug() << "dropEvent:" << path;
    //发送文件名出去
    emit SignalSendPlayPath(path);
}

void MyLabel::mouseDoubleClickEvent(QMouseEvent *){
    if(isFull) {
        isFull = false;
        emit SignalSetFullShow(isFull);
    } else {
        isFull = true;
        emit SignalSetFullShow(isFull);
    }
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "mythread.h"
#include "mylabel.h"
#include <QFileDialog>
#include <QProcess>
#include <QTimer>
#include <QKeyEvent>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
protected:
    void keyPressEvent(QKeyEvent *event);

private slots:
    void on_btn_open_clicked();
    void on_btn_play_clicked();
    void on_btn_preview_clicked();

    void on_btn_next_clicked();
    void on_dial_voice_valueChanged(int value);
    void on_horizontalSlider_seek_valueChanged(int value);

    void doProcessTimeout();
    void doProcessReadyRead();
    void doProcessDrayPlay(const QString filename);//dray play
    void doProcessFullScreen(bool);//toggle fullScreen.

    void on_horizontalSlider_seek_sliderPressed();
    void on_horizontalSlider_seek_sliderReleased();

private:
    Ui::MainWindow *ui;
    void Init();
    void playMovie(const QString filename);
    void setMyWindowStatus(bool);

    MyLabel *lbl;
    QStringList playList;
    int playIndex;
    QProcess *playProcess;
    int isPlaying;//0:undefine,1:playing, 2:stop,4:end
    QTimer *myTimer;
    bool isSeek; //是否 人为拖动进度条
    bool isFull;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"

//实现文件拖拽进入,打印出文件信息
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Init();
}
MainWindow::~MainWindow()
{
    delete ui;
}
//Functions
void MainWindow::Init(){
    lbl = new MyLabel(this);
    lbl->setMinimumSize(QSize(0,230));
    ui->gridLayout->addWidget(lbl, 0,0, 1,1);
    connect(lbl, SIGNAL(SignalSendPlayPath(const QString)),
            this, SLOT(doProcessDrayPlay(const QString)));
    connect(lbl, SIGNAL(SignalSetFullShow(bool)),
            this, SLOT(doProcessFullScreen(bool)));

    playList.clear();
    playIndex = 0;
    isPlaying = -1;
    isSeek = false;
    isFull = false;

    playProcess = new QProcess(this);
    myTimer = new QTimer(this);
    connect(myTimer, SIGNAL(timeout()),
            this, SLOT(doProcessTimeout()));
    connect(playProcess, SIGNAL(readyRead()),
            this, SLOT(doProcessReadyRead()));
}

//播放功能
void MainWindow::playMovie(const QString filename){
    //后台播放 mplayer, 命令的形式播放
    QString program = "/usr/bin/mplayer";
    QStringList arguments;
    arguments << filename;
    arguments << "-wid"; //在指定窗口中播放
    arguments << QString::number(lbl->winId());
    arguments << "-slave"; //set mplayer mode slave
    arguments << "-quiet"; //mplayer变为静默模式
    playProcess->close(); //stop preview video
    playProcess->start(program, arguments);
    myTimer->start(1000);

    isPlaying = 1;
    ui->btn_play->setIcon(QIcon(":/new/prefix1/image2/pause.png"));
    QString cmd2 = QString("volume %1 1\n")
            .arg(ui->dial_voice->value());
    playProcess->write(cmd2.toLatin1());
    ui->horizontalSlider_seek->setValue(0);
}
void MainWindow::setMyWindowStatus(bool flag){
    isFull = flag;
    if(flag){
        this->showFullScreen();
        ui->horizontalSlider_seek->hide();
        //Frame can hide/show it.
        //        ui->frame->hide();
    }else{
        this->showNormal();
        ui->horizontalSlider_seek->show();
        //        ui->frame->show();
    }
}

//Functions End

//slots
void MainWindow::doProcessFullScreen(bool fullScreen){
    setMyWindowStatus(fullScreen);
}

void MainWindow::doProcessDrayPlay(const QString filename){
    this->playList.clear();
    this->playList.append(filename);
    playIndex=0;
    playMovie(playList.at(playIndex));
}

void MainWindow::doProcessTimeout(){
    QString cmd = "get_percent_pos\n";//send cmd query progress.
    playProcess->write(cmd.toLatin1());
}
void MainWindow::doProcessReadyRead(){ // callback
    //qDebug() << playProcess->readAll();//"ANS_PERCENT_POSITION=99\n"
    QString str = QString(playProcess->readAll());
    if(str.contains("ANS_PERCENT_POSITION")){
        str.replace("\n", "");
        QString var = str.split("=").at(1);
        int progress = var.toUInt();
        ui->horizontalSlider_seek->setValue(progress);
        lbl->setText(var);
    }
}


void MainWindow::on_btn_open_clicked()
{
    QStringList array = QFileDialog::getOpenFileNames(this, "需要播放的文件",
                                                      "/home/k2/Videos", "video(*.mp4)");
    if(array.length()<=0) return;
    playList = array;
    playIndex = 0;
    QString name = playList.at(playIndex);
    playMovie(name);
}
void MainWindow::on_btn_play_clicked()
{
    if(isPlaying == -1) return;
    QString iconName;
    QString cmd = "pause\n";
    if(isPlaying == 1){//原来播放中,马上暂停 play->stop
        isPlaying = 2;
        iconName = ":/new/prefix1/image2/pause.png";
    } else if(isPlaying == 2){//stop->play
        isPlaying = 1;
        iconName = ":/new/prefix1/image2/play.png";
    }
    ui->btn_play->setIcon(QIcon(iconName));
    playProcess->write(cmd.toLatin1());//send cmd
}
//上一集,下一集
void MainWindow::on_btn_preview_clicked()
{
    if(playIndex > 0){
        playIndex--;
        playMovie(playList.at(playIndex));
    }
}
void MainWindow::on_btn_next_clicked()
{
    if(playIndex < playList.length()-1){
        playIndex++;
        playMovie(playList.at(playIndex));
    }
}
//"volume 50 1\n"
void MainWindow::on_dial_voice_valueChanged(int value)
{
    qDebug() << "value:" << value;
    if(isPlaying ==1 || isPlaying ==2) {
        QString cmd = QString("volume %1 1\n").arg(value);
        playProcess->write(cmd.toLatin1());
    }
}
//快进快退
void MainWindow::on_horizontalSlider_seek_valueChanged(int value)
{
    if(isSeek){
        if(isPlaying ==1 || isPlaying ==2) {
            QString cmd = QString("seek %1 1\n").arg(value);
            playProcess->write(cmd.toLatin1());

        }
    }
}
void MainWindow::on_horizontalSlider_seek_sliderPressed()
{
    isSeek=true;
}
void MainWindow::on_horizontalSlider_seek_sliderReleased()
{
    isSeek=false;
}
//slots End

//Events
void MainWindow::keyPressEvent(QKeyEvent* event){
    //key():AsciiCode. 键盘放大、缩小;
    //text():显示的键值
    qDebug() << event->key() << event->text();
    int keyCode = event->key();
    if(keyCode == 16777216){//Esc
        if(isFull){
            setMyWindowStatus(false);
        }
    }
    if(event->text()== "f" || event->text()== "F"){
        if(!isFull){
            setMyWindowStatus(true);
        }
    }
    //组合键
    Qt::KeyboardModifiers ms=event->modifiers();
    if(ms==Qt::ShiftModifier){
        qDebug()<<"ShiftModifier";
    }else if(ms==Qt::ControlModifier){
        qDebug()<<"ControlModifier";
    }
}
//Events end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 颜料进口清关全流程攻略 | 外贸小白必看! 颜料进口清关流程涉及多个环节,具体步骤及注意事项如下: 一、基本流程 ...
    百航Amy阅读 73评论 0 0
  • 致良知线上良知班学习第129天 时间:2025年5月21日 姓名: 地区: 志愿:我立志成为一名自省利他致良知的印...
    静恒定阅读 53评论 0 0
  • 东南亚智能售货机年增长20%!哪些城市会是下一个爆发点? 东南亚智能售货机市场正以年复合增长率19.8%高速扩张(...
    会展阿柯阅读 68评论 0 0
  • 黄酒药方大全包括多种针对不同健康问题的药方,以下是部分药方的详细介绍: 下乳——黄酒鲜虾汤:准备新鲜大虾100克、...
    a43016b733d5阅读 50评论 0 0
  • 焦点解决高效教练在解决家庭问题时的时间框架 作者:高德明 2017年11月 高效焦点解决教练(SFBC)是一个与时...
    高德明教育学阅读 91评论 0 0

友情链接更多精彩内容