效果图:
方法一:QMediaPlayer+QVideoWidget (如果其他格式视频播放不出来,安装一个视频解码器可以解决)
#include "QMutPlayer.h"
#include "ui_QMutPlayer.h"
#include<QFileDialog>
#include<QDebug>
QMutPlayer::QMutPlayer(QWidget *parent) :
QWidget(parent),
ui(new Ui::QMutPlayer)
{
ui->setupUi(this);
mVideoPlayer = new QMediaPlayer(this);
mSurfacePlayer = new QMediaPlayer(this);
mVideoWidget = new QVideoWidget(ui->lab_videowidget);
mVideoPlayer->setVideoOutput(mVideoWidget);
}
QMutPlayer::~QMutPlayer()
{
delete ui;
}
void QMutPlayer::on_btn_select_video_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"", tr("video (*.mp4 *.wav *.avi)"));
if(fileName.isEmpty())
return;
mVideoPlayer->setMedia(QUrl(fileName));
mVideoPlayer->play();
}
void QMutPlayer::showEvent(QShowEvent *event)
{
Q_UNUSED(event)
mVideoWidget->resize(ui->lab_videowidget->size());
}
方法二:QMediaPlayer+ QAbstractVideoSurface 提取视频帧
然后将视频帧转化为QImage,用QML 和QLabel 加载,或者QWidget 的paintEvent 绘画出来
//提取视频帧
#ifndef VIDEOSURFACE_H
#define VIDEOSURFACE_H
#include <QObject>
#include<QAbstractVideoSurface>
class VideoSurface : public QAbstractVideoSurface
{
Q_OBJECT
public:
explicit VideoSurface(QObject *parent = 0);
signals:
void showImage(QImage img);
protected:
bool present(const QVideoFrame &frame);
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;
};
#endif // VIDEOSURFACE_H
#include "VideoSurface.h"
#include<QPixmap>
VideoSurface::VideoSurface(QObject *parent) : QAbstractVideoSurface(parent)
{
}
QList<QVideoFrame::PixelFormat> VideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
Q_UNUSED(handleType);
QList<QVideoFrame::PixelFormat> listPixelFormats;
listPixelFormats << QVideoFrame::Format_RGB32
<< QVideoFrame::Format_BGR32;
return listPixelFormats;
}
bool VideoSurface::present(const QVideoFrame &frame)
{
if (frame.isValid())
{
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
QImage recvImage(cloneFrame.bits(), cloneFrame.width(), cloneFrame.height(), QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
cloneFrame.unmap();
emit showImage(recvImage);
return true;
}
return false;
}
//调用
mSurfacePlayer = new QMediaPlayer(this);
mVideoSurface = new VideoSurface(this);
mSurfacePlayer->setVideoOutput(mVideoSurface);
connect(mVideoSurface,SIGNAL(showImage(QImage)),SLOT(onShowImage(QImage)));
/QML 刷新图片 需要用到QQuickImageProvider
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#include <QQuickImageProvider>
class ImageProvider : public QQuickImageProvider
{
public:
explicit ImageProvider();
public:
void addImage(QImage &image);
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
private:
QImage mImage;
};
#endif // IMAGEPROVIDER_H
#include "ImageProvider.h"
ImageProvider::ImageProvider(): QQuickImageProvider(QQuickImageProvider::Image)
{
}
void ImageProvider::addImage(QImage &image)
{
mImage = image;
}
QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
return QPixmap::fromImage(mImage);
}
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
return mImage;
}
#ifndef SHOWIMAGE_H
#define SHOWIMAGE_H
#include <QObject>
#include"ImageProvider.h"
class ShowImage : public QObject
{
Q_OBJECT
public:
static ShowImage *instance();
~ShowImage();
ImageProvider *m_pImgProvider;
void setImage(QImage image);
signals:
void callQmlRefeshImg();
private:
ShowImage(QObject *parent = 0);
static ShowImage *mShowImage;
};
#endif // SHOWIMAGE_H
#include "ShowImage.h"
ShowImage* ShowImage::mShowImage = NULL;
ShowImage:: ShowImage(QObject *parent) : QObject(parent)
{
m_pImgProvider = new ImageProvider();
}
ShowImage *ShowImage::instance()
{
if(mShowImage == NULL)
mShowImage = new ShowImage;
return mShowImage;
}
ShowImage::~ShowImage()
{
delete m_pImgProvider;
}
void ShowImage::setImage(QImage image)
{
m_pImgProvider->addImage(image);
emit callQmlRefeshImg();
}
#ifndef QMLLOADWIDGET_H
#define QMLLOADWIDGET_H
#include <QWidget>
#include<QQuickWidget>
class QmlLoadWidget : public QQuickWidget
{
Q_OBJECT
public:
explicit QmlLoadWidget(QWidget *parent = 0);
signals:
void errMessage(QString msg);
public slots:
void addImage(QImage image);
void getErr(QQuickWindow::SceneGraphError error, const QString &message);
};
#endif // QMLLOADWIDGET_H
#include "QmlLoadWidget.h"
#include<QQmlEngine>
#include<QQmlContext>
#include"ShowImage.h"
QmlLoadWidget::QmlLoadWidget(QWidget *parent) : QQuickWidget(parent)
{
this->resize(parent->size());
setResizeMode(QQuickWidget::SizeRootObjectToView);
rootContext()->setContextProperty("CodeImage",ShowImage::instance());
engine()->addImageProvider("CodeImg",ShowImage::instance()->m_pImgProvider);
setSource(QUrl("qrc:/QmlRender/LoadVideImage.qml"));
}
void QmlLoadWidget::addImage(QImage image)
{
ShowImage::instance()->setImage(image);
}
void QmlLoadWidget::getErr(QQuickWindow::SceneGraphError error, const QString &message)
{
emit errMessage(message);
}
完整代码下载地址:https://download.csdn.net/download/xzpblog/10960022