利用QT自带的multimedia制作简单的视频播放器

由于Qt自带multimedia播放的视频格式现在基本都无法使用,所以这里需要安装一个LAV Video可以解码视频。
其pro文件需要添加

QT       += core gui  multimedia multimediawidgets

重载QSlider鼠标响应事件,实现单击跳转至任意位置
重载mouseMovedEvent事件:
利用QT自带的multimedia制作简单的视频播放器_第1张图片
customslider.h

fndef CUSTOMSLIDER_H
#define CUSTOMSLIDER_H

#include
#include
#include
class CustomSlider : public QSlider
{
    Q_OBJECT
public:
    explicit CustomSlider(QWidget *parent = 0):QSlider(parent)
    {

    }
protected:
    void mousePressEvent(QMouseEvent*ev);
signals:
    void costomSliderClicked();//自定义的鼠标信号,用于捕获处理
};
#endif // CUSTOMSLIDER_H

#include “customslider.h”

void CustomSlider::mousePressEvent(QMouseEvent *ev)
{
    //注意应先调用父类鼠标点击处理事件,这样可以不影响拖动的情况
        QSlider::mousePressEvent(ev);
        //获取鼠标位置,这里并不能直接从ev中取值
        double pos=ev->pos().x()/(double)width();
        setValue(pos*(maximum()-minimum())+minimum());
        //发送自定义的鼠标单击信号
        emit costomSliderClicked();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "customslider.h"
#include
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

protected:
    void mouseDoubleClickEvent(QMouseEvent *event);
private slots:


    void on_pushButton_open_file_clicked();

    void on_pushButton_play_and_pause_clicked();

    void onTimerOut();
    void slider_progress_clicked();
    void slider_progress_moved();
    void slider_progress_released();
    //void slider_volume_changed();
    void on_pushButton_volume_clicked();


    void on_state_slider_volume_sliderMoved(int position);

private:
    Ui::Widget *ui;
    //播放视频全局变量
    QVBoxLayout *layout_video;//布局
    QMediaPlayer *player;//播放器
    QVideoWidget *widget;//视频播放控件
    //播放状态  true为播放,false为暂停
    bool play_state;
    //是否重新载入视频
    bool if_reload=false;
    //与Slider有关的播放控制音量
    QTimer *timer;
    int maxValue;//视频slider最大值
    bool state_volume;//音量slider是否有效
     int  max_volume;//最大音量
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //视频控制
    maxValue=1000;
    //在载入ui时,先禁用播放/暂停按钮和音量按钮
    ui->pushButton_play_and_pause->setEnabled(false);
    ui->pushButton_volume->setEnabled(false);
    ui->slider_progress->setEnabled(false);
    //点击
 connect(ui->slider_progress,&CustomSlider::costomSliderClicked,this,&Widget::slider_progress_clicked);
   //移动
 connect(ui->slider_progress,&CustomSlider::sliderMoved,this,&Widget::slider_progress_moved);
   //释放
 connect(ui->slider_progress,&CustomSlider::sliderReleased,this,&Widget::slider_progress_released);
 //音量控制
 max_volume=100;
 state_volume=true;
 ui->state_slider_volume->setEnabled(false);
 ui->pushButton_volume->setEnabled(false);
}
Widget::~Widget()
{
    delete ui;
}
//双击全屏
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
   if(!this->isFullScreen())
   {
       this->showFullScreen();
   }
   else
   {
      this->showNormal();
   }
}

void Widget::on_pushButton_open_file_clicked()
{
     QString filename=QFileDialog::getOpenFileName(this,tr("Open file"),QDir::homePath(),tr("Multimedia files()"));
    //选择视屏文件
//    QString filename=QFileDialog::getOpenFileName(this,
//                                   QString::fromLocal8Bit("选择视频文件"),QDir::homePath(),
//                                   QString::fromLocal8Bit("视频格式(*.avi*.mp4*.flv*.mkv)"));
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(NULL,"Title","Content",
                                 QMessageBox::Ok);
        return ;
    }
    //重新加载视频时,重置以下变量
   if(if_reload)
    {
       delete layout_video;
       delete player;
       delete widget;
       delete timer;
    }
   if_reload=true;

   //实例化三个全局变量
   layout_video=new QVBoxLayout;
   player=new QMediaPlayer;
   widget=new QVideoWidget;
   //设置视频播放区域与label的边距
   layout_video->setMargin(0);
   //根据label_player尺寸设置播放区域
   widget->resize(ui->label_player->size());
   layout_video->addWidget(widget);
   ui->label_player->setLayout(layout_video);
   player->setVideoOutput(widget);

   //设置播放器
   player->setMedia(QUrl::fromLocalFile(filename));//设置需要打开的媒体文件
    //player_state为true表示播放,false表示暂停
   play_state=true;
   //启用播放/暂停按钮,并将其文本设置为"暂停"
   ui->pushButton_play_and_pause->setEnabled(true);
   ui->pushButton_volume->setEnabled(true);
   ui->pushButton_play_and_pause->setText(QString::fromLocal8Bit("暂停"));
   player->play();

   //启用slider_progress并设置范围
   ui->slider_progress->setEnabled(true);
   ui->slider_progress->setRange(0,maxValue);
   timer=new QTimer();
   timer->setInterval(1000);
   timer->start();
   //将timer连接至onTimerOut槽函数
   connect(timer,SIGNAL(timeout()),this,SLOT(onTimerOut()));
   //音量方面
   //启用state_slider_volume并设置范围
   ui->pushButton_volume->setEnabled(true);
   ui->state_slider_volume->setEnabled(true);
   ui->state_slider_volume->setRange(0,max_volume);
   ui->state_slider_volume->setValue(max_volume);
}

void Widget::on_pushButton_play_and_pause_clicked()
{
    //反转播状态
    if(play_state)
    {
        player->pause();
        ui->pushButton_play_and_pause->setText(QString::fromLocal8Bit("播放"));
    }
    else
    {
        player->play();
        ui->pushButton_play_and_pause->setText(QString::fromLocal8Bit("暂停"));

    }
    play_state=!play_state;
}

void Widget::onTimerOut()
{
    //目前的位置除以总时长
    ui->slider_progress->setValue(player->position()*maxValue/player->duration());
}

void Widget::slider_progress_clicked()
{
    player->setPosition(ui->slider_progress->value()*player->duration()/maxValue);
}

void Widget::slider_progress_moved()
{
    //暂停定时器,在用户拖动过程中不修改slider的值
    timer->stop();
    player->setPosition(ui->slider_progress->value()*player->duration()/maxValue);

}

void Widget::slider_progress_released()
{
    //用户释放滑块后,重启定时器
    timer->start();
}

void Widget::on_pushButton_volume_clicked()
{
    qint64 temp=ui->state_slider_volume->value();
    if(state_volume)
    {player->setVolume(0);
        ui->pushButton_volume->setText(QString::fromLocal8Bit("音量"));
        state_volume=false;
                }
    else
    {
        player->setVolume(temp);
         ui->pushButton_volume->setText(QString::fromLocal8Bit("静音"));
         state_volume=true;
    }
}
//利用Moved来控制音量
void Widget::on_state_slider_volume_sliderMoved(int position)
{

    player->setVolume(position);
}

利用QT自带的multimedia制作简单的视频播放器_第2张图片
这里补充一下ui_widget.h
因为当时直接使用的Qt Create在UI界面上直接进行的设计,写博客没有关注这一点。

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

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

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QGridLayout *gridLayout;
    QLabel *label_player;
    QVBoxLayout *verticalLayout;
    CustomSlider *slider_progress;
    QHBoxLayout *horizontalLayout;
    QSpacerItem *horizontalSpacer;
    QPushButton *pushButton_open_file;
    QPushButton *pushButton_play_and_pause;
    QPushButton *pushButton_volume;
    QSlider *state_slider_volume;
    QSpacerItem *horizontalSpacer_2;
    QSpacerItem *verticalSpacer;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QStringLiteral("Widget"));
        Widget->resize(926, 593);
        Widget->setStyleSheet(QStringLiteral("background-color: rgb(20, 20, 20);"));
        gridLayout = new QGridLayout(Widget);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QStringLiteral("gridLayout"));
        label_player = new QLabel(Widget);
        label_player->setObjectName(QStringLiteral("label_player"));
        label_player->setStyleSheet(QStringLiteral(""));

        gridLayout->addWidget(label_player, 0, 0, 1, 1);

        verticalLayout = new QVBoxLayout();
        verticalLayout->setSpacing(6);
        verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
        slider_progress = new CustomSlider(Widget);
        slider_progress->setObjectName(QStringLiteral("slider_progress"));
        slider_progress->setOrientation(Qt::Horizontal);
        slider_progress->setTickPosition(QSlider::NoTicks);

        verticalLayout->addWidget(slider_progress);

        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setSpacing(6);
        horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
        horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        horizontalLayout->addItem(horizontalSpacer);

        pushButton_open_file = new QPushButton(Widget);
        pushButton_open_file->setObjectName(QStringLiteral("pushButton_open_file"));
        pushButton_open_file->setStyleSheet(QStringLiteral("background-color: rgb(255, 170, 0);"));

        horizontalLayout->addWidget(pushButton_open_file);

        pushButton_play_and_pause = new QPushButton(Widget);
        pushButton_play_and_pause->setObjectName(QStringLiteral("pushButton_play_and_pause"));
        pushButton_play_and_pause->setStyleSheet(QStringLiteral("background-color: rgb(255, 170, 127);"));

        horizontalLayout->addWidget(pushButton_play_and_pause);

        pushButton_volume = new QPushButton(Widget);
        pushButton_volume->setObjectName(QStringLiteral("pushButton_volume"));
        pushButton_volume->setStyleSheet(QStringLiteral("background-color: rgb(255, 255, 255);"));

        horizontalLayout->addWidget(pushButton_volume);

        state_slider_volume = new QSlider(Widget);
        state_slider_volume->setObjectName(QStringLiteral("state_slider_volume"));
        state_slider_volume->setOrientation(Qt::Horizontal);
        state_slider_volume->setTickPosition(QSlider::NoTicks);
        state_slider_volume->setTickInterval(0);

        horizontalLayout->addWidget(state_slider_volume);

        horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        horizontalLayout->addItem(horizontalSpacer_2);


        verticalLayout->addLayout(horizontalLayout);


        gridLayout->addLayout(verticalLayout, 2, 0, 1, 1);

        verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Minimum);

        gridLayout->addItem(verticalSpacer, 1, 0, 1, 1);


        retranslateUi(Widget);

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QApplication::translate("Widget", "Widget", Q_NULLPTR));
        label_player->setText(QString());
        pushButton_open_file->setText(QApplication::translate("Widget", "\346\211\223\345\274\200\346\226\207\344\273\266", Q_NULLPTR));
        pushButton_play_and_pause->setText(QApplication::translate("Widget", "\346\222\255\346\224\276", Q_NULLPTR));
        pushButton_volume->setText(QApplication::translate("Widget", "\351\235\231\351\237\263", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

你可能感兴趣的:(利用QT自带的multimedia制作简单的视频播放器)