1.效果图
2.代码实现
2.1 .pro文件
cpp
QT += core gui multimedia multimediawidgets
2.2 .h文件
cpp
#ifndef VIDEOPLAYING_H
#define VIDEOPLAYING_H
#include <QWidget>
#include<QFileDialog>
#include<QMediaPlayer>
#include<QMediaRecorder>
#include<QVideoWidget>
#include <QWidget>
#include <QHBoxLayout> //水平布局
#include <QVBoxLayout> //垂直布局
#include <QVideoWidget> //显示视频
#include <QMediaPlayer> //播放声音
#include <QPushButton> //按钮
#include <QSlider> //滑动条
#include <QStyle> //设置图标
#include <QFileDialog> //选择文件/文件夹
#include<QAudioOutput>
namespace Ui {
class VideoPlaying;
}
class VideoPlaying : public QWidget
{
Q_OBJECT
public:
explicit VideoPlaying(QWidget *parent = nullptr);
~VideoPlaying();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void updateSliderPosition();
void on_horizontalSlider_sliderPressed();
void on_horizontalSlider_sliderMoved(int position);
void on_horizontalSlider_sliderReleased();
private:
Ui::VideoPlaying *ui;
QVideoWidget * m_videoWidget;//输出设备
QMediaPlayer * m_player; // 声明 QMediaPlayer 对象
QString m_video_filePath;
// 设置音频角色为视频
QAudioOutput* m_audioOutput;// 创建音频输出对象
qint64 m_time;
bool is_play;
bool is_slider;
QString getTime(qint64 time);
};
#endif // VIDEOPLAYING_H
2.3 .cpp文件
cpp
#include "videoplaying.h"
#include "ui_videoplaying.h"
VideoPlaying::VideoPlaying(QWidget *parent)
: QWidget(parent)
, ui(new Ui::VideoPlaying)
{
ui->setupUi(this);
m_player = new QMediaPlayer(this); // 初始化 QMediaPlayer 对象
m_videoWidget = new QVideoWidget(this);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_videoWidget);
ui->widget->setLayout(layout);
m_player->setVideoOutput(m_videoWidget);
m_audioOutput=new QAudioOutput(this);
m_player->setAudioOutput(m_audioOutput); // 设置音频输出
connect(m_player, &QMediaPlayer::positionChanged, this, &VideoPlaying::updateSliderPosition);
}
VideoPlaying::~VideoPlaying()
{
delete ui;
}
//选择视频
void VideoPlaying::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, "选择视频", "", "Video Files (*.mp4 *.avi *.mkv)");
if (!filename.isEmpty()) {
qDebug() << "选择的视频文件路径:" << filename;
m_video_filePath=filename;
m_player->setSource(m_video_filePath);
m_time= m_player->duration(); // 获取视频时长,单位为毫秒
ui->lineEdit->setText(m_video_filePath);
ui->label_all->setText(getTime(m_time));
ui->label_now->setText("00:00:00");
}
}
//播放
void VideoPlaying::on_pushButton_2_clicked()
{
if (!m_video_filePath.isEmpty()) {
QUrl videoUrl = QUrl::fromLocalFile(m_video_filePath);
m_player->play(); // 开始播放视频
} else {
qDebug() << "请先选择视频文件";
}
}
//暂停
void VideoPlaying::on_pushButton_3_clicked()
{
// m_player->stop();
m_player->pause(); // 暂停播放
}
//前进
void VideoPlaying::on_pushButton_4_clicked()
{
qint64 position = m_player->position(); // 获取当前播放位置
position += 5000; // 向后退5秒(单位为毫秒)
if (position < 0) {
position = 0; // 不能倒退到负值
}
m_player->setPosition(position); // 设置视频播放位置
}
//后退
void VideoPlaying::on_pushButton_5_clicked()
{
qint64 position = m_player->position(); // 获取当前播放位置
position -= 5000; // 向后退5秒(单位为毫秒)
if (position < 0) {
position = 0; // 不能倒退到负值
}
m_player->setPosition(position); // 设置视频播放位置
}
void VideoPlaying::updateSliderPosition()
{
qint64 duration = m_player->duration(); // 视频总时长
qint64 position=m_player->position();//当前时长
if (duration > 0) {
int sliderPosition = static_cast<int>(position * 100 / duration);
if(!is_slider){
ui->horizontalSlider->setValue(sliderPosition); // 更新滑块的位置
}
ui->label_now->setText(getTime(position)); // 更新当前播放时间
}
}
QString VideoPlaying::getTime(qint64 time)
{
int hours = (time / (1000 * 60 * 60)) % 24;
int minutes = (time / (1000 * 60)) % 60;
int seconds = (time / 1000) % 60;
qDebug() << "视频时长:" << hours << "小时" << minutes << "分钟" << seconds << "秒";
QString allTime=QString::number(hours)+":"+QString::number(minutes)+":"+QString::number(seconds);
return allTime;
}
void VideoPlaying::on_horizontalSlider_sliderPressed()
{
is_slider=true;
}
void VideoPlaying::on_horizontalSlider_sliderMoved(int position)
{
if(is_slider) {
qint64 duration = m_player->duration(); // 视频总时长,单位为毫秒
qint64 newPos = position * duration / 100; // 根据滑块位置计算当前播放位置,单位为毫秒
m_player->setPosition(newPos); // 设置视频播放位置
ui->label_now->setText(getTime(newPos)); // 更新当前播放时间
}
}
void VideoPlaying::on_horizontalSlider_sliderReleased()
{
is_slider=false;
}