1.效果图
2.代码实现
2.1 .pro文件
QT += core gui multimedia multimediawidgets
2.2 .h文件
#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_OBJECTpublic: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文件
#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;}