本篇文章记录:qt 每隔一段时间自动弹出到屏幕最前端播放视频(视频自己配置路径),键盘上键代表开/关,下键代表进入循环播放
主ui (mainwindow.ui)里把下面自己创建的提示为窗口部件
![](https://i-blog.csdnimg.cn/blog_migrate/0eeb54b09532dc932dfaaeb1cf58c0f8.png)
mainwindow.h 不需要写,.cpp也可以不写,下面我写的是改变程序exe图标
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ico 资源路径
this->setWindowIcon(QIcon("://pp.ico"));
}
MainWindow::~MainWindow()
{
delete ui;
}
注意://pp.ico 是我自己添加的.exe图标文件,首先添加qrc文件然后把路径复制过来即可
创建一个新的ui 文件(video)实现视频的播放
![](https://i-blog.csdnimg.cn/blog_migrate/77bdf17a4baa4f8af6b7cc084a8ed6cf.png)
video.h文件
#ifndef VIDEO_H
#define VIDEO_H
#include <QWidget>
#include <QtMultimedia>
#include <QVideoWidget>
#include <Windows.h>
#include <QTime>
#include <QDebug>
#include "../../PublicLibrary/config.h"
namespace Ui {
class Video;
}
class Video : public QWidget
{
Q_OBJECT
public:
explicit Video(QWidget *parent = nullptr);
~Video();
bool isLoop;
QTimer *tim;
void MaxWindowFun();
void MinWindowFun();
config*con=nullptr;
public slots:
void SmediaStatusChanged(QMediaPlayer::MediaStatus status);
private:
QVideoWidget * m_pPlayerWidget;
QMediaPlayer * m_pPlayer;
Ui::Video *ui;
HHOOK keyboardHook;
public slots:
void key_up();
void key_down();
};
class keymonitor : public QObject
{
Q_OBJECT
private:
static keymonitor*m_keymonitor;
private:
keymonitor()
{
}
public:
~keymonitor()
{
};
static keymonitor*getinstance()
{
if(m_keymonitor==nullptr)
{
m_keymonitor=new keymonitor();
}
return m_keymonitor;
};
static LRESULT LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT* ks = (KBDLLHOOKSTRUCT*)lParam;
if (ks->flags == 128 || ks->flags == 129 /*|| ks->flags == 0*/)
{
if (ks->vkCode == 38)
{
emit keymonitor::getinstance()->winkey_up();
}
else if (ks->vkCode == 40)
{
emit keymonitor::getinstance()->winkey_down();
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
};
signals:
void winkey_up();
void winkey_down();
};
#endif // VIDEO_H
video.cpp文件
#include "video.h"
#include "ui_video.h"
keymonitor* keymonitor::m_keymonitor = nullptr;
Video::Video(QWidget *parent) :
QWidget(parent),
ui(new Ui::Video)
{
//配置文件初始化
con=new config;
ui->setupUi(this);
setFocusPolicy(Qt::ClickFocus);
isLoop=false;
m_pPlayer = new QMediaPlayer;
m_pPlayer->setSource(QUrl(con->videoPath));
m_pPlayerWidget = new QVideoWidget;
//播放视频
m_pPlayer->setVideoOutput(m_pPlayerWidget);
//开启音频
m_pPlayer->setAudioOutput(new QAudioOutput());
//添加视频在ui框中播放
ui->verticalLayout->addWidget(m_pPlayerWidget);
//填充背景色
m_pPlayerWidget->setAutoFillBackground(true);
QPalette qplte;
qplte.setColor(QPalette::Window, QColor(0,0,0));
m_pPlayerWidget->setPalette(qplte);
//播放
connect(keymonitor::getinstance(),SIGNAL(winkey_up()), this, SLOT(key_up()));
//停止
connect(keymonitor::getinstance(),SIGNAL(winkey_down()), this, SLOT(key_down()));
//连接
connect(m_pPlayer,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(SmediaStatusChanged(QMediaPlayer::MediaStatus)));
//播放
m_pPlayer->play();
//初始化勾子
keyboardHook = 0;
keyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
&keymonitor::LowLevelKeyboardProc,
GetModuleHandleA(NULL),
NULL
);
tim = new QTimer();
}
Video::~Video()
{
delete m_pPlayer;
delete m_pPlayerWidget;
delete ui;
}
//最大窗口化
void Video::MaxWindowFun()
{
this->parentWidget()->showFullScreen();
this->activateWindow();
this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
m_pPlayer->play();
}
//最小窗口化
void Video::MinWindowFun()
{
m_pPlayer->stop();
this->parentWidget()->showMinimized();
}
//窗口状态改变时进行开/关/循环判断
void Video::SmediaStatusChanged(QMediaPlayer::MediaStatus status)
{
if(QMediaPlayer::EndOfMedia==status)
{
if(isLoop)
{
MinWindowFun();
tim->setInterval(con->delayVideo);
connect(tim,&QTimer::timeout,this,[=](){
MaxWindowFun();
tim->stop();
});
tim->start();
}
else
{
MinWindowFun();
}
}
}
//键盘上
void Video::key_up()
{
if(this->parentWidget()->isMinimized())
{
MaxWindowFun();
isLoop=false;
}
else if(this->parentWidget()->isFullScreen())
{
MinWindowFun();
}
}
//键盘下
void Video::key_down()
{
MaxWindowFun();
isLoop=true;
}
创建config类文件,设置路径配置文件 config.h
#ifndef CONFIG_H
#define CONFIG_H
#include <QObject>
class config : public QObject
{
Q_OBJECT
public:
int delayVideo;
QString videoPath;
//json文件函数
void jsonFile();
explicit config(QObject *parent = nullptr);
signals:
};
#endif // CONFIG_H
config.cpp
#include "config.h"
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
config::config(QObject *parent)
: QObject{parent}
{
jsonFile();
}
void config::jsonFile()
{
//项目文件下创建配置文件
QFile file("./configInfo.json");
//如果不小删除了会自动生成此文件格式
if(!file.open(QIODevice::ReadOnly))
{
QVariantHash Data;
//播放路径
Data.insert("Mp4_Path", "./1.mp4");
//延迟时间
Data.insert("Delay_time", 2000);
QJsonObject rootObj = QJsonObject::fromVariantHash(Data);
QJsonDocument document;
document.setObject(rootObj);
QByteArray byte_array = document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
//根据实际填写路径
QFile file("./configInfo.json");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug() << "file error!";
}
QTextStream in(&file);
in << json_str;
file.close(); // 关闭file
}
QJsonParseError jerr;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &jerr);
QJsonObject jroot = doc.object();
//读取文件
delayVideo = jroot["Delay_time"].toInt();
videoPath = jroot["Mp4_Path"].toString();
qInfo()<<"shuchu"<<delayVideo<<" "<<videoPath;
}
主函数main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QSharedMemory>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSharedMemory shared_memory;
//防止程序多启
shared_memory.setKey(QString("666666"));//设置固定共享内存段的key值
if(shared_memory.attach()) //尝试将进程附加到该共享内存段
{
return 0;
}
if(shared_memory.create(1)) //创建1byte的共享内存段
{
MainWindow w;
w.activateWindow();
w.setWindowState((w.windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
w.show();
//w.resize(1920, 1080);
w.showMaximized();
w.showFullScreen();
return a.exec();
}
}