QT进阶日记004

多媒体

一、项目加

QT +=...... multimedia multimediaWidgets

二、创建一个QWidget项目

三、布局一个界面

添加一个显示视频的容器Widget容器到界面来。playWindow

添加播放,暂停,结束,快近,快退,等按钮QPushButton

btnPlay,btnPause,btnEnd,btnFront,btnBack等命名

添加一个进度条QSlider当播放进度条,添加一个当音量条。

sliPlay,sliSound

添加一个标签QLabel,显示总时间长/当前位置时间,laVol

这样的话,界面元素基本上就够了。

四、写代码

1、引用

#include<QMediaPlayer>//播放器类

#include<QMediaDevices>//多媒体设备

#include<QAudioOutput>//音频输出设备

#include<QvideoWidget>//视频输出窗口

在H文件里定义一个

QMediaPlayer * m_play;

在cpp里的初始化

在构造函数里就行了

MyPlayer :: MyPlayer(QWidget * parent):QWidget(parent),ui(new Ui :: Widget),m_play(new QMediaPlayer(this))

{

ui->setupUi(this);

//设置播放来源

m_play->setSource(QUrl("文件/地/址/file.mp3"));//有mp3,mp4

连接看看错误状态等信号

connect(m_player,&QMediaPlayer :: errorOccureed,this,=(QMediaPlayer :: Error error,const QString $str){

qDebug()<<str;

});

//关联信号,得到时长变化

connect(m_player,&QMediaPlayer :: durationChanged,this,\[\](qint64 duration){

当改变时长时,触发这个处理过程,该时间是毫秒单位。

ui->laVol->setText(QString ("%1:%2:%3").arg(duration/1000/60/60,2,10,QChar('0')).arg(duration/1000/60,2,10,QChar('0')).arg(duration/1000%60,2,10,QChar('0'));

});

//设定音频输出设备

m_play->setAudiaOutput(new QAudioOutput(this));

//视频输出设备

QVideoWidget wg=new QVideoWidget(this);

包装一个层

QHBoxlayout * lay=new QHBoxLayout(ui->playWindow);

lay->addWidget(wg);

m_play->setVideoOutput(wg);

m_play->play();

}

MyPlayer :: ~MyPlayer()

{

delete ui;

}

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript