【在网上找到了Qt Creator中播放视频的代码,并且运行成功,但是由于本人需要放在VS的Qt插件中运行,所以将Qt中的代码改了一丢丢放在了VS中运行】
原来代码链接:
https://blog.csdn.net/liji_digital/article/details/83691035
https://download.csdn.net/download/liji_digital/10762790
这个链接的代码是在Qt Creator中的项目,无法直接在VS上使用
所以自己修改成了在VS上使用的代码:如下
新建VS项目——Qt GUI Application——选择需要的模块(如图1)——选择基类模块以及名字(如图2)
点击项目——添加——类——按照图片输入(如图3)
新建以后注意到,VS界面应该是这样的:
点击VideoPlayer.ui进行布局(如图4)
三个PushButton按钮:一个播放Play,一个停止Stop,一个加载Load。
一个进度条按钮:Input Widget中的horizontalSlider按钮
一个放视频的背景:Layout里面的verticalLayout
拖到界面以后进行布局:三个按钮和进度条按钮先水平布局,然后整体一个全局布局
然后修改各个按钮的名字,并将进度条提升为PlayerSlider类中
开始写代码吧!!!
PlayerSlider中的代码:
#ifndef PLAYERSLIDER_H
#define PLAYERSLIDER_H
#include <QSlider>
#include <QMouseEvent>
class PlayerSlider : public QSlider
{
Q_OBJECT
public:
PlayerSlider(QWidget * parent = 0);
void setProgress(qint64);
signals:
void sigProgress(qint64);
private:
bool m_bPressed;
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
};
#endif // PLAYERSLIDER_H
PlayerSlider.cpp中的代码:
#include "PlayerSlider.h"
PlayerSlider::PlayerSlider(QWidget * parent) : QSlider(parent)
{
m_bPressed = false;
}
void PlayerSlider::mousePressEvent(QMouseEvent *e)
{
m_bPressed = true;
QSlider::mousePressEvent(e);//必须有这句,否则手动不能移动滑块
}
void PlayerSlider::mouseMoveEvent(QMouseEvent *e)
{
QSlider::mouseMoveEvent(e);//必须有这句,否则手动不能移动滑块
}
void PlayerSlider::mouseReleaseEvent(QMouseEvent *e)
{
m_bPressed = false;
qint64 i64Pos = value();
emit sigProgress(i64Pos);
QSlider::mouseReleaseEvent(e);//必须有这句,否则手动不能移动滑块
}
void PlayerSlider::setProgress(qint64 i64Progress)
{
if (!m_bPressed)
setValue(i64Progress);
}
VedioPlayer.h中的代码:
#pragma once
#include <QtWidgets/QWidget>
#include "ui_VideoPlayer.h"
#include <QtMultimedia>
#include <QVideoWidget>
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
VideoPlayer(QWidget *parent = Q_NULLPTR);
bool m_bReLoad;
public slots:
void OnSetMediaFile(void);
void OnSlider(qint64);
void OnDurationChanged(qint64);
void OnStateChanged(QMediaPlayer::State);
private:
Ui::VideoPlayerClass ui;
QVideoWidget * m_pPlayerWidget;
QMediaPlayer * m_pPlayer;
};
VedioPlayer.cpp中的代码:
#include "VideoPlayer.h"
#include <QFileDialog>
#include "ui_VideoPlayer.h"
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_pPlayer = new QMediaPlayer;
m_pPlayerWidget = new QVideoWidget;
m_pPlayer->setVideoOutput(m_pPlayerWidget);
ui.verticalLayout->addWidget(m_pPlayerWidget);
m_pPlayerWidget->setAutoFillBackground(true);
QPalette qplte;
qplte.setColor(QPalette::Window, QColor(0, 0, 0));
m_pPlayerWidget->setPalette(qplte);
//载入
connect(ui.BtnLoad, SIGNAL(clicked()), this, SLOT(OnSetMediaFile()));
//播放
connect(ui.BtnPlay, SIGNAL(clicked()), m_pPlayer, SLOT(play()));
//停止
connect(ui.BtnStop, SIGNAL(clicked()), m_pPlayer, SLOT(stop()));
connect(m_pPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));
ui.BtnStop->setEnabled(false);
//设置滑块行为
m_bReLoad = true;
ui.slider->setEnabled(false);
//ui.slider->setEnabled(false);
connect(m_pPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(OnSlider(qint64)));
connect(m_pPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(OnDurationChanged(qint64)));
connect(ui.slider, SIGNAL(sigProgress(qint64)), m_pPlayer, SLOT(setPosition(qint64)));
}
void VideoPlayer::OnSetMediaFile(void)
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
if (!fileNames.empty())
{
m_pPlayer->setMedia(QUrl::fromLocalFile(fileNames[0]));
m_bReLoad = true;
ui.slider->setValue(0);
}
}
void VideoPlayer::OnSlider(qint64 i64Pos)
{
ui.slider->setProgress(i64Pos);
//ui.horizontalSlider->setProgress(i64Pos);
}
void VideoPlayer::OnDurationChanged(qint64 i64Duration)
{
if (i64Duration > 0 && m_bReLoad)
{
ui.slider->setRange(0, i64Duration);
m_bReLoad = false;
}
}
void VideoPlayer::OnStateChanged(QMediaPlayer::State enumState)
{
if (QMediaPlayer::StoppedState == enumState)
{
ui.BtnPlay->setEnabled(true);
ui.BtnStop->setEnabled(false);
ui.slider->setEnabled(false);
}
else if (QMediaPlayer::PlayingState == enumState)
{
ui.BtnPlay->setEnabled(false);
ui.BtnStop->setEnabled(true);
ui.slider->setEnabled(true);
}
}
main.cpp中的代码:
#include "VideoPlayer.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoPlayer w;
w.show();
return a.exec();
}
运行结果:
但是只能对AVI格式的视频读取。
qt在播放视频时出错:
DirectShowPlayerService::doRender: Unresolved error code 80040266
这个时候是支持avi的,但是不支持mp4 MOV格式的视频
解决方法:可以下载安装k-Lite codec pack 就可以播放了
原文:https://blog.csdn.net/summermaoz/article/details/53812222
由于自己电脑是win7的,所以下载了14.20版本的,选择了normal版本的,然后一直Next安装了,不好的是不小心安装到了C盘。