qt简单视频播放器

#ifndef PLAYERSLIDER_H
#define PLAYERSLIDER_H
 
   
#include 
#include 
class PlayerSlider:public QSlider
{
    Q_OBJECT
public:
    PlayerSlider(QWidget *parent = 0);
    void setProgress(qint64);
signals:
    void sigProgress(qint64);
private:
    bool m_bPressed;
protected:
    void mouseMoveEvent(QMouseEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
};
 
   
#endif // PLAYERSLIDER_H
 
  

#include "playerslider.h"
 
   
PlayerSlider::PlayerSlider(QWidget* parent):QSlider(parent)
{
    m_bPressed = false;
 
   
}
 
   
void PlayerSlider::setProgress(qint64 i64Progress)
{
    if(!m_bPressed)
        setValue(i64Progress);
 
   
}
 
   
void PlayerSlider::mouseMoveEvent(QMouseEvent *e)
{
    QSlider::mouseMoveEvent(e);//必须有这句话,否则手动不能移动滑块
 
   
}
 
   
void PlayerSlider::mousePressEvent(QMouseEvent *e)
{
    m_bPressed = true;
    QSlider::mousePressEvent(e);//必须有这句话,否则手动不能移动滑块
}
 
   
void PlayerSlider::mouseReleaseEvent(QMouseEvent *e)
{
    m_bPressed = false;
    qint64 i64Pos = value();
    emit sigProgress(i64Pos);
    QSlider::mouseReleaseEvent(e);//必须有这句话,否则手动不能移动滑块
 
   
}
 
  

#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
 
   
#include 
#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class VideoPlayer; }
QT_END_NAMESPACE
 
   
class VideoPlayer : public QWidget
{
    Q_OBJECT
 
   
public:
    VideoPlayer(QWidget *parent = nullptr);
    ~VideoPlayer();
    bool m_bReLoad;
public slots:
    void OnSetMediaFile(void);
    void OnSlider(qint64);
    void OnDurationChanged(qint64);
    void OnStateChange(QMediaPlayer::State);
 
   
private:
    QVideoWidget *m_pPlayerWidget;
    QMediaPlayer *m_pPlayer;
    Ui::VideoPlayer *ui;
};
#endif // VIDEOPLAYER_H
 
  

#include "videoplayer.h"
#include "ui_videoplayer.h"
#include 
#include 
#include 
#include 
#include 
#include 
//#include "playerslider.h"
VideoPlayer::VideoPlayer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VideoPlayer)
{
    ui->setupUi(this);
    m_pPlayer = new QMediaPlayer;
    m_pPlayerWidget = new QVideoWidget;
    m_pPlayer->setVideoOutput(m_pPlayerWidget);
    ui->verticalLayout->addWidget(m_pPlayerWidget);
 
   
    m_pPlayerWidget->setAutoFillBackground(true);
    QPalette qplte;
    qplte.setColor(QPalette::Window,QColor(0,0,0));
    m_pPlayerWidget->setPalette(qplte);
    connect(ui->load,&QPushButton::clicked,this,&VideoPlayer::OnSetMediaFile);
    connect(ui->play,&QPushButton::clicked,m_pPlayer,&QMediaPlayer::play);
    connect(ui->stop,&QPushButton::clicked,m_pPlayer,&QMediaPlayer::stop);
    connect(m_pPlayer,&QMediaPlayer::stateChanged,this,&VideoPlayer::OnStateChange);
    ui->stop->setEnabled(false);
    m_bReLoad =true;
    ui->platersider->setEnabled(false);
    connect(m_pPlayer,&QMediaPlayer::positionChanged,this,&VideoPlayer::OnSlider);
    connect(m_pPlayer,&QMediaPlayer::durationChanged,this,&VideoPlayer::OnDurationChanged);
    connect(ui->platersider,SIGNAL(sigProgress(qint64)),m_pPlayer,SLOT(setPosition(qint64)));
}
 
   
VideoPlayer::~VideoPlayer()
{
    delete m_pPlayer;
    delete m_pPlayerWidget;
    delete ui;
}
 
   
void VideoPlayer::OnSetMediaFile(void)
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QStringList filenames;
    if(dialog.exec())
        filenames = dialog.selectedFiles();
    if(!filenames.empty())
    {
        m_pPlayer->setMedia(QUrl::fromLocalFile(filenames[0]));
        m_bReLoad = true;
        ui->platersider->setValue(0);
 
   
    }
    qDebug()< 
   
 
   
}
 
   
void VideoPlayer::OnSlider(qint64 i64Pos)
{
    ui->platersider->setProgress(i64Pos);
 
   
}
 
   
void VideoPlayer::OnDurationChanged(qint64 i64Duration)
{
 
   
 
   
    if(i64Duration>0&&m_bReLoad)
    {
        ui->platersider->setRange(0,i64Duration);
        m_bReLoad = false;
    }
}
 
   
void VideoPlayer::OnStateChange(QMediaPlayer::State enumState)
{
    if(QMediaPlayer::StoppedState==enumState)
    {
        ui->play->setEnabled(true);
        ui->stop->setEnabled(false);
        ui->platersider->setEnabled(false);
 
   
    }
    else if(QMediaPlayer::PlayingState==enumState)
    {
        ui->play->setEnabled(false);
        ui->stop->setEnabled(true);
        ui->platersider->setEnabled(true);
    }
 
   
}
 
  
 
 

你可能感兴趣的:(qt)