#include "videorecorder.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QUrl>
#include <QCoreApplication>
#include <QVideoSink>
VideoRecorder::VideoRecorder(QWidget *parent)
: QMainWindow(parent)
{
createLayout();
camera = new QCamera(QMediaDevices::defaultVideoInput());
audioInput = new QAudioInput(this);
recorder = new QMediaRecorder(this);
mediaCaptureSession = new QMediaCaptureSession(this);
mediaCaptureSession->setAudioInput(audioInput);
mediaCaptureSession->setRecorder(recorder);
mediaCaptureSession->setVideoOutput(videoWidget);
recorder->setQuality(QMediaRecorder::HighQuality);
recorder->setOutputLocation(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + "/demo.mp4"));
recorder->setEncodingMode(QMediaRecorder::ConstantQualityEncoding);
recorder->setAudioChannelCount(recorder->audioChannelCount());
connect(recorder,&QMediaRecorder::durationChanged,this,&VideoRecorder::onDurationChanged);
cameraDevices = QMediaDevices::videoInputs();
comboBoxInput->addItem("<Node>");
foreach (const QCameraDevice & cam, cameraDevices) {
comboBoxInput->addItem(cam.description());
}
connect(comboBoxInput,&QComboBox::currentIndexChanged,this,&VideoRecorder::selectCameraDevice);
}
void VideoRecorder::createLayout()
{
label1 = new QLabel(tr("Recorded Time(s):"));
labelTime = new QLabel;
labelTime->setFrameStyle(QFrame::Box);
btnClose = new QPushButton(tr("关闭"));
btnStart = new QPushButton(tr("开始"));
btnStop = new QPushButton(tr("停止"));
btnPause = new QPushButton(tr("暂停"));
comboBoxInput = new QComboBox;
videoWidget = new QVideoWidget;
connect(btnStart,&QPushButton::clicked,this,&VideoRecorder::on_btn_start_clicked);
connect(btnPause,&QPushButton::clicked,this,&VideoRecorder::on_btn_pause_clicked);
connect(btnStop,&QPushButton::clicked,this,&VideoRecorder::on_btn_stop_clicked);
connect(btnClose,&QPushButton::clicked,this,&VideoRecorder::on_btn_close_clicked);
QVBoxLayout *vTopRight = new QVBoxLayout;
vTopRight->addWidget(comboBoxInput);
QGridLayout *gLayoutMain = new QGridLayout;
gLayoutMain->addWidget(videoWidget,0,0,5,3);
gLayoutMain->addLayout(vTopRight,0,3);
gLayoutMain->addWidget(btnStart,1,3);
gLayoutMain->addWidget(btnPause,2,3);
gLayoutMain->addWidget(btnStop,3,3);
gLayoutMain->addWidget(label1,5,0);
gLayoutMain->addWidget(labelTime,5,1,1,2);
gLayoutMain->addWidget(btnClose,5,3);
resize(480,320);
setMinimumSize(480,320);
setMaximumSize(480,320);
setWindowTitle(tr("视频录像机"));
QWidget *wi = new QWidget;
wi->setLayout(gLayoutMain);
setCentralWidget(wi);
}
void VideoRecorder::onDurationChanged(qint64 duration){
labelTime -> setText(QString("%1 s").arg(QString::number(duration / 1000)));
}
void VideoRecorder::selectCameraDevice(int )
{
//遍历当前选择的设备是否等于可利用的设备,然后进行设备的配置
for(const QCameraDevice &cam : cameraDevices){
if(cam.description() == comboBoxInput->currentText()){
camera -> setCameraDevice(cam);
mediaCaptureSession -> setCamera(camera);
camera -> start();
break;
}
}
}
void VideoRecorder::on_btn_start_clicked(){
recorder -> record();
}
void VideoRecorder::on_btn_pause_clicked(){
recorder -> pause();
}
void VideoRecorder::on_btn_stop_clicked(){
recorder -> stop();
labelTime -> setText("0");
}
void VideoRecorder::on_btn_close_clicked(){
close();
}
VideoRecorder::~VideoRecorder() {}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
VideoRecorder.rar (5个子文件)
VideoRecorder
videorecorder.h 1KB
main.cpp 189B
VideoRecorder.pro 618B
VideoRecorder.pro.user 18KB
videorecorder.cpp 4KB
共 5 条
- 1
资源评论
金博客
- 粉丝: 107
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功