注意:
1. 在本文中的代码,使用的时”块应用“给出,而不是直接采用”代码“的形式给出。因此,在直接拷贝代码时,由于字符编码问题,可能导致编译错误现象。典型的表现,就是:拉丁空格被置换为中文空格。
2. 之所以采用”块应用“,是因为这种方式不会引起折叠,方便代码的阅读。
-------------------------------------------------------
关联参考:
qt视频播放界面类设计(有源码**)
https://blog.csdn.net/ken2232/article/details/135601449
Qt视频播放器[QMediaPlayer+QVideowidget] (有源码**)
https://blog.csdn.net/ken2232/article/details/135598822
摄像头标定:原理、方法、步骤 (**)
https://blog.csdn.net/ken2232/article/details/135644249
===================================
qt视频播放界面类设计(有源码**)
0、显示效果
1、设计思路
使用qt制作播放器方法有很多,但是不使用第三方库,则只能使用QVideoWidget类+QMediaPlayer类方法(这里话说太满了,容易被打脸)
QVideoWidget类是一个继承QWidget的界面类,用于视频显示。
QMediaPlayer类用于解析音频文件和视频文件,继承自QMediaObject。QMediaObject可以提供关于媒体内容的接入,通过URL进行初始化,同时也可以提供可以播放的媒体文件的列表,与QMediaplayer对象息息相关。
qt视频播放界面类VideoWidget,则是集合了QVideoWidget类+QMediaPlayer类,将该搭档组合在了一起,在VideoWidget类中实例化,并设置外部视频播放控制所必要的接口,如播放、暂停、停止、设置播放速度、设置播放视频等等(具体看源代码中h文件)。
既然是界面类设计,所以VideoWidget是继承QWidget类,后期应用也是使用QWidget提升。
2、源代码
h文件:
#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H#include <QObject>
#include <QWidget>
#include <QVideoWidget>
#include <QMediaPlayer>class VideoWidget : public QWidget
{
Q_OBJECT
public:
explicit VideoWidget(QWidget *parent = nullptr);
~VideoWidget();
void setVideoName(QString name);
QString getVideoName(); //获取导入的视频名字void init(); //播放初始化,初始化一些状态
bool play(); //启动,无设置视频名情况下失败返回0
bool pause();
bool stop(); //关闭,无播放情况下失败返回0
void setPlaySpeed(double speed); //设置播放速度quint64 getDuration(); //获取视频时长
bool setPlayPos(quint64 position);
quint64 getCurrentPlyPos();void setVideoBackColor(QColor color);
signals:
void stateChanged(QMediaPlayer::State); //播放状态改动
void positionChanged(qint64); //播放位置改动
void durationChanged(qint64); //播放时间改动private slots:
void OnStateChanged(QMediaPlayer::State enumState);private:
QVideoWidget *mVideoWidget;
QMediaPlayer *mPlayer;
QString videoName;
QMediaPlayer::State mState;void resizeEvent(QResizeEvent *event);
};#endif // VIDEOWIDGET_H
cpp文件:
#include "videowidget.h"
VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{
mVideoWidget = new QVideoWidget(this);
mPlayer = new QMediaPlayer;
mPlayer->setVideoOutput(mVideoWidget); //设置视频输出显示位置//设置视频外的背景为黑色
mVideoWidget->setAutoFillBackground(true);
QPalette palette(QPalette::Window, QColor(0,0,0));
mVideoWidget->setPalette(palette);connect(mPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(OnStateChanged(QMediaPlayer::State)));
connect(mPlayer,SIGNAL(positionChanged(qint64)),this,SIGNAL(positionChanged(qint64)));
connect(mPlayer,SIGNAL(durationChanged(qint64)),this,SIGNAL(durationChanged(qint64)));
}VideoWidget::~VideoWidget()
{
delete mPlayer;
delete mVideoWidget;
}void VideoWidget::init()
{}
void VideoWidget::setVideoName(QString name)
{
mPlayer->setMedia(QUrl::fromLocalFile(name)); //给播放器设置视频路劲
videoName = name;
}
QString VideoWidget::getVideoName()
{
return videoName;
}bool VideoWidget::play()
{
if(videoName.isEmpty())
return 0;
mPlayer->play();
return 1;
}
bool VideoWidget::pause()
{
if(mState != QMediaPlayer::PlayingState)
return 0;
mPlayer->pause();
return 1;
}
bool VideoWidget::stop()
{
if(mState == QMediaPlayer::StoppedState)
return 0;
mPlayer->stop();
return 1;
}void VideoWidget::setPlaySpeed(double speed)
{
mPlayer->setPlaybackRate(speed); //设置播放速度
}quint64 VideoWidget::getDuration()
{
return mPlayer->duration();
}
bool VideoWidget::setPlayPos(quint64 position)
{
if(mState == QMediaPlayer::StoppedState)
return 0;
mPlayer->setPosition(position);
return 1;
}
quint64 VideoWidget::getCurrentPlyPos()
{
return mPlayer->position(); //返回播放位置值
}void VideoWidget::setVideoBackColor(QColor color)
{
QPalette palette(QPalette::Window, color);
this->setPalette(palette);
}void VideoWidget::OnStateChanged(QMediaPlayer::State enumState)
{
mState = enumState; //获取状态
emit stateChanged(enumState); //对外发送状态
}void VideoWidget::resizeEvent(QResizeEvent *event)
{
mVideoWidget->setGeometry(0,0,this->width(),this->height());
}
3、demo设计
pro文件记得添加模块:
QT += core gui multimedia multimediawidgets
mainwindows界面设计如下:
然后对按钮等控件都添加信号槽,widget提升为设计好的VideoWidget类
h文件就不贴了,(注:mainwindows.h 文件中,需要假如 pushbotton 的槽函数声明)
mainwindows.cpp代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_select_clicked()
{
QFileDialog dialog(this); //打开一个文件选择窗口
dialog.setFileMode(QFileDialog::AnyFile); //设置任意文件可选
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles(); //获取选择的文件链表if(!fileNames.empty())
ui->widget->setVideoName(fileNames.at(0)); //给播放器设置视频路劲
}void MainWindow::on_pushButton_start_clicked()
{
ui->widget->play();
}void MainWindow::on_pushButton_pause_clicked()
{
ui->widget->pause();
}void MainWindow::on_pushButton_stop_clicked()
{
ui->widget->stop();
}void MainWindow::on_comboBox_speed_currentIndexChanged(int index)
{
if(index == 0) ui->widget->setPlaySpeed(0.5);
else if(index == 1) ui->widget->setPlaySpeed(0.75);
else if(index == 2) ui->widget->setPlaySpeed(0.1);
else if(index == 3) ui->widget->setPlaySpeed(1.25);
else if(index == 4) ui->widget->setPlaySpeed(1.5);
else if(index == 5) ui->widget->setPlaySpeed(2);
}
————————————————
版权声明:本文为CSDN博主「码肥人壮」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42887343/article/details/115327064