我想实现一个将在 QML 上显示的 QMediaPlayer 视频流。我试图扩展 QMediaPlayer 类并在 MyMediaPlayer 类下创建。我在我的 main.cpp 文件中实例化这个类以在 GUI 上显示它。即使没有错误,视频在 QML 窗口中也不可见。我的错在哪里?
我的媒体播放器.cpp
#include "MyMediaPlayer.h"
MyMediaPlayer::MyMediaPlayer(QObject* parent, Flags flags): QMediaPlayer(parent, flags)
{
}
void MyMediaPlayer::setVideoSurface(QAbstractVideoSurface* surface)
{
qDebug() << "Changing surface";
m_surface = surface;
setVideoOutput(m_surface);
}
QAbstractVideoSurface* MyMediaPlayer::getVideoSurface()
{
qDebug() << "Surface Got";
return m_surface;
}
我的媒体播放器.h
#ifndef MYMEDIAPLAYER_H
#define MYMEDIAPLAYER_H
#include <QMediaPlayer>
class MyMediaPlayer: public QMediaPlayer
{
Q_OBJECT
Q_PROPERTY(QAbstractVideoSurface* videoSurface READ getVideoSurface WRITE setVideoSurface )
public:
MyMediaPlayer(QObject * parent = 0, Flags flags = 0);
public slots:
//virtual void play(const QString& strFile);
void setVideoSurface(QAbstractVideoSurface* surface);
QAbstractVideoSurface* getVideoSurface();
private:
QAbstractVideoSurface* m_surface;
};
#endif // MYMEDIAPLAYER_H
主文件
MyMediaPlayer* player = new MyMediaPlayer();
engine.rootContext()->setContextProperty("mediaplayer", player);
QQuickView view;
view.engine()->rootContext()->setContextProperty("mediaplayer", player);
player->setMedia(QUrl(QStringLiteral("qrc:/1.mp4")));
qDebug() << "media set";
player->play();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
main.qml
VideoOutput {
z: 1000
id: videooutput
width: 320
height: 240
source: mediaplayer
}