1.我们需要的头文件
2. .cpp文件
#include "videowin.h"
#include "ui_videowin.h"
#include <QFileDialog>
#include <QTime>
#include <QMessageBox>
#include <QFont>
videowin::videowin(QWidget *parent) :
QWidget(parent),
ui(new Ui::videowin)
{
m_pWidget = new QWidget(this);
m_pWidget->setGeometry(0, 0, 1152, 616);
QPalette pal(m_pWidget->palette());
//设置背景黑色
pal.setColor(QPalette::Background, Qt::black);
m_pWidget->setAutoFillBackground(true);
m_pWidget->setPalette(pal);
m_pWidget->show();
ui->setupUi(this);
this->setWindowIcon(QIcon(":/icon_video/video.png"));
this->setWindowTitle("大鱼播放器");
this->i = 0;
this->n = 0;
this->j = 0;
ui->verticalSlider->hide();
ui->listWidget->hide();
//this->player->setVolume(position);
this->videoWidget = new QVideoWidget(this);
this->videoWidget->setGeometry(50,40,720,470);//播放视频大小位置
this->player = new QMediaPlayer(this);
this->ui->label->setText("00:00/00:00");//显示时间的
this->ui->label->setStyleSheet("color:white");
this->ui->label_2->setStyleSheet("color:white");
this->ui->label_2->setFont(QFont("阿里妈妈数黑体",16));
this->timer = new QTimer(this);//定时器
this->ui->pushButton->setIcon(this->style()->standardPixmap(QStyle::SP_MediaPlay));//给按键设置类型
//this->time = new QTime(this);
connect(this->ui->listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(changeMovie(QListWidgetItem *)));
connect(this->timer,SIGNAL(timeout()),this,SLOT(updateTime_1()));
connect(this->player,SIGNAL(durationChanged(qint64)),this,SLOT(getDuration_1(qint64)));
connect(this->player,SIGNAL(positionChanged(qint64)),this,SLOT(setSliderValue(qint64)));
}
videowin::~videowin()
{
delete ui;
}
//获取文件信息
void videowin::on_pushButton_7_clicked()
{
//this->namelist.clear();
//this->ui->listWidget->clear();
this->namelist =QFileDialog::getOpenFileNames(this,"选择",".","WMV(*.wmv);;AVI(*.avi);;MP4(*.MP4)");
for(int i = 0;i<namelist.size();i++)
{
QFileInfo fileInfos(namelist.at(i));//获取文件信息
QListWidgetItem *item = new QListWidgetItem(fileInfos.fileName(),this->ui->listWidget);
item->setToolTip(namelist.at(i));
this->ui->listWidget->addItem(item);
}
}
//双击能实现播放视频
void videowin::changeMovie(QListWidgetItem *item)
{
this->index = this->ui->listWidget->currentRow();
//设置播放器要播放的视频
this->player->setMedia(QUrl(item->toolTip()));
//获取视频名字
this->name = item->text()+" ";
this->ui->label_2->setText(this->name);
QFont ft;
ft.setPointSize(14);
this->ui->label_2->setFont(ft);
//播放
this->player->setVideoOutput(this->videoWidget);
this->player->play();
this->ui->pushButton->setIcon(this->style()->standardPixmap(QStyle::SP_MediaPause));
this->timer->start(1000);
}
//暂停键
void videowin::on_pushButton_clicked()
{
int state = this->player->state();//获取播放器的状态
if(state == QMediaPlayer::PausedState)//暂停
{
this->player->play();
this->ui->pushButton->setIcon(this->style()->standardPixmap(QStyle::SP_MediaPause));
}
else if(state == QMediaPlayer::PlayingState)//播放中
{
this->player->pause();
this->ui->pushButton->setIcon(this->style()->standardPixmap(QStyle::SP_MediaPlay));
}
}
//显示视频进度和时长
void videowin::updateTime_1()
{
this->n++;//时间一到,显示名称的一半
if(this->n == this->name.size())
{
this->n = 0;
}
this->ui->label_2->setText(this->name.mid(this->n));//显示名称
qint64 dur = this->player->duration();//获取到总时长
qint64 pos = this->player->position();//获取当前的播放位置
QTime time1(0,0,0,0),time2(0,0,0,0);//显示:转成 00:00
this->ui->label->setText(time1.addMSecs(pos).toString("mm:ss") +"/" +time2.addMSecs(dur).toString("mm:ss"));
}
void videowin::getDuration_1(qint64 duration)
{
qDebug()<<duration;
this->ui->horizontalSlider->setRange(0,duration);//设置进度条范围
this->ui->horizontalSlider->setValue(0);
}
//视频滑块
void videowin::on_horizontalSlider_sliderMoved(int position)
{
this->player->setPosition(position);//根据滑块的位置设置播放音乐的位置
}
//下集
void videowin::on_pushButton_2_clicked()
{
if(this->namelist.empty())
{
QMessageBox::information(this,"提示","没有可播放的视频");
}
else
{
this->index++;
if(this->index == this->namelist.size())
{
QMessageBox::information(this,"提示","已经是最后一个视频了");
this->index--;
}
this->player->stop();
QFileInfo fileInfo(this->namelist.at(this->index));
this->name = fileInfo.fileName()+" ";
this->ui->label->setText(this->name);
this->player->setMedia(QUrl(this->namelist.at(this->index)));
this->player->play();
}
}
//控制音量条
void videowin::on_pushButton_4_clicked()
{
if(i == 0)
{
ui->verticalSlider->show();
i = 1;
}
else if( i == 1)
{
ui->verticalSlider->hide();
i = 0;
}
}
//调节音量
void videowin::on_verticalSlider_sliderMoved(int position)
{
this->player->setVolume(position);
}
//更新视频滑块
void videowin::setSliderValue(qint64 position)
{
this->ui->horizontalSlider->setValue(position);
}
//选集按钮
void videowin::on_pushButton_5_clicked()
{
if(j == 0)
{
ui->listWidget->show();
j = 1;
}
else if( j == 1)
{
ui->listWidget->hide();
j = 0;
}
}
3.ui界面
文章来源地址https://www.toymoban.com/news/detail-521535.html文章来源:https://www.toymoban.com/news/detail-521535.html
到了这里,关于QT实现视频播放器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!