我希望实现一个将在QML上显示的QMediaPlayer视频流。我试图扩展QMediaPlayer类并在MyMediaPlayer类下面创建。我在main.cpp文件中实例化该类以在GUI上显示它。即使没有错误,视频在QML窗口上也不可见。我的错在哪里?
mymediaplayer.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;
}
mymediaplayer.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
的main.cpp
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
}