话不多说先上图:
实现了一个基本的播放器的例子,实现出最近本的项目,项目比较基础,喜欢的朋友,可呀通过这些简单的例子,实现更为复杂的视屏播放器,比如一些客户端。
不多说了来看看代码吧:
工程文件使用的:
QT += multimedia
QT += multimediawidgets
对应的头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include "qmyvideowidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QMediaPlayer *player;//视频播放器
QString durationTime;//
QString positionTime;//
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//自定义槽函数
void onstateChanged(QMediaPlayer::State state);
void onDurationChanged(qint64 duration);
void onPositionChanged(qint64 position);
//
void on_btnAdd_clicked();
void on_btnPlay_clicked();
void on_btnPause_clicked();
void on_btnStop_clicked();
// void on_listWidget_doubleClicked(const QModelIndex &index);
// void on_btnClear_clicked();
void on_sliderVolumn_valueChanged(int value);
void on_btnSound_clicked();
// void on_sliderPosition_valueChanged(int value);
// void on_sliderPosition_sliderMoved(int position);
void on_sliderPosition_valueChanged(int value);
// void on_btnRemove_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
对应的cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
//#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);//创建视频播放器
player->setNotifyInterval(2000);//信息更新周期
player->setVideoOutput(ui->videoWidget);//视频显示组件
ui->videoWidget->setMediaPlayer(player);//设置显示组件的关联播放器
connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(onstateChanged(QMediaPlayer::State)));
connect(player,SIGNAL(positionChanged(qint64)),
this, SLOT(onPositionChanged(qint64)));
connect(player,SIGNAL(durationChanged(qint64)),
this, SLOT(onDurationChanged(qint64)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onstateChanged(QMediaPlayer::State state)
{//播放器状态变化
ui->btnPlay->setEnabled(!(state==QMediaPlayer::PlayingState));
ui->btnPause->setEnabled(state==QMediaPlayer::PlayingState);
ui->btnStop->setEnabled(state==QMediaPlayer::PlayingState);
}
void MainWindow::onDurationChanged(qint64 duration)
{//文件时长变化
ui->sliderPosition->setMaximum(duration);
int secs=duration/1000;//秒
int mins=secs/60; //分钟
secs=secs % 60;//余数秒
durationTime=QString::asprintf("%d:%d",mins,secs);
ui->LabRatio->setText(positionTime+"/"+durationTime);
}
void MainWindow::onPositionChanged(qint64 position)
{//文件播放位置变化
if (ui->sliderPosition->isSliderDown())
return; //如果正在拖动滑条,退出
ui->sliderPosition->setSliderPosition(position);//
int secs=position/1000;//秒
int mins=secs/60; //分钟
secs=secs % 60;//余数秒
positionTime=QString::asprintf("%d:%d",mins,secs);
ui->LabRatio->setText(positionTime+"/"+durationTime);
}
void MainWindow::on_btnAdd_clicked()
{//打开文件
QString curPath=QDir::homePath();//获取系统当前目录
QString dlgTitle="选择视频文件"; //对话框标题
QString filter="wmv文件(*.wmv);;mp4文件(*.mp4);;所有文件(*.*)"; //文件过滤器
QString aFile=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
if (aFile.isEmpty())
return;
QFileInfo fileInfo(aFile);
ui->LabCurMedia->setText(fileInfo.fileName());
player->setMedia(QUrl::fromLocalFile(aFile));//设置播放文件
player->play();
}
void MainWindow::on_btnPlay_clicked()
{//播放
player->play();
}
void MainWindow::on_btnPause_clicked()
{//暂停
player->pause();
}
void MainWindow::on_btnStop_clicked()
{//停止
player->stop();
}
void MainWindow::on_sliderVolumn_valueChanged(int value)
{//调节音量
player->setVolume(value);
}
void MainWindow::on_btnSound_clicked()
{//静音按钮
bool mute=player->isMuted();
player->setMuted(!mute);
if (mute)
ui->btnSound->setIcon(QIcon(":/images/images/volumn.bmp"));
else
ui->btnSound->setIcon(QIcon(":/images/images/mute.bmp"));
}
void MainWindow::on_sliderPosition_valueChanged(int value)
{//播放位置
player->setPosition(value);
}
void MainWindow::on_pushButton_clicked()
{//全屏按钮
ui->videoWidget->setFullScreen(true);
}
自定义视频显示组件 QmyVideoWidget
#ifndef QMYVIDEOWIDGET_H
#define QMYVIDEOWIDGET_H
#include <QObject>
#include <QWidget>
#include <QVideoWidget>
#include <QMediaPlayer>
class QmyVideoWidget : public QVideoWidget
{
private:
QMediaPlayer *thePlayer;//
protected:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
public:
QmyVideoWidget(QWidget *parent = Q_NULLPTR);
void setMediaPlayer(QMediaPlayer *player);
};
#endif // QMYVIDEOWIDGET_H
对应的cpp文件:
#include "qmyvideowidget.h"
#include <QKeyEvent>
#include <QMouseEvent>
void QmyVideoWidget::keyPressEvent(QKeyEvent *event)
{//按键事件,ESC退出全屏状态
if ((event->key() == Qt::Key_Escape)&&(isFullScreen()))
{
setFullScreen(false);
event->accept();
QVideoWidget::keyPressEvent(event);
}
}
void QmyVideoWidget::mousePressEvent(QMouseEvent *event)
{//鼠标事件,单击控制暂停和继续播放
if (event->button()==Qt::LeftButton)
{
if (thePlayer->state()==QMediaPlayer::PlayingState)
thePlayer->pause();
else
thePlayer->play();
}
QVideoWidget::mousePressEvent(event);
}
QmyVideoWidget::QmyVideoWidget(QWidget *parent):QVideoWidget(parent)
{
}
void QmyVideoWidget::setMediaPlayer(QMediaPlayer *player)
{//设置播放器
thePlayer=player;
}
例子连接:
链接:https://pan.baidu.com/s/11R3FfGJxVtH54vMC4FvJlA
提取码:5oa1
使用QGraphicsVideoltem 显示视屏跟这个区别不太大,就不写博客了,留下例子,大家学习下:
链接:https://pan.baidu.com/s/19kp2qZ_es19G0-EBF-E5og
提取码:o6to