嘿,堆栈目前正在尝试创建一些只从本地源播放视频文件的基本代码(试图让它工作然后尝试并实现rtsp)。下面是我目前的代码。如果有人能给我一个想法,我会出错,这将是伟大的。
目前代码失败并显示错误
C:\ Qt \ Qt5.1.0 \ 5.1.0 \ msvc2010_opengl \ examples \ multimediawidgets \ videographicsitem \ main.cpp:58:错误:C2664:'QGraphicsScene :: addItem':无法从'QGraphicsVideoItem *'转换参数1到'QGraphicsItem *' 指向的类型是无关的;转换需要reinterpret_cast,C风格的转换或函数式转换
我认为我必须对插入到additem的变量进行类型转换。但是不知道如何用两个Qclasses做到这一点。如果有人有一个基本播放器的例子,它只包含一个视频帧并播放第二个你启动该程序(最好不是播放按钮)。如果有人有类似的东西,请让我知道,因为这将使我的生活更轻松。 Main.cpp的
#include "videoplayer.h"
#include <QApplication>
#include <QGraphicsView>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
//VideoPlayer player;
//QGraphicsView graphicsView;
QMediaPlayer *player ;
QGraphicsVideoItem *item;
QGraphicsView *graphicsView;
QGraphicsScene *scene;
player->setVideoOutput(item);
// scene->addItem(item);
graphicsView->scene()->addItem(item);
graphicsView->show();
player->setMedia(QUrl("rtsp://192.168.100.58:8554/stream"));
player->play();
graphicsView->show();
return app.exec();
}
Videoplayer.cpp
#include "videoplayer.h"
#include <QtWidgets>
#include <QVideoSurfaceFormat>
#include <QGraphicsVideoItem>
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent)
, mediaPlayer(0, QMediaPlayer::VideoSurface)
, videoItem(0)
, playButton(0)
, positionSlider(0)
{
videoItem = new QGraphicsVideoItem;
videoItem->setSize(QSizeF(640, 480));
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *graphicsView = new QGraphicsView(scene);
scene->addItem(videoItem);
QSlider *rotateSlider = new QSlider(Qt::Horizontal);
rotateSlider->setRange(-180, 180);
rotateSlider->setValue(0);
connect(rotateSlider, SIGNAL(valueChanged(int)),
this, SLOT(rotateVideo(int)));
QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
playButton = new QPushButton;
playButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()),
this, SLOT(play()));
positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, 0);
connect(positionSlider, SIGNAL(sliderMoved(int)),
this, SLOT(setPosition(int)));
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addWidget(playButton);
controlLayout->addWidget(positionSlider);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(graphicsView);
layout->addWidget(rotateSlider);
layout->addLayout(controlLayout);
setLayout(layout);
mediaPlayer.setVideoOutput(videoItem);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
//QString fileName = "rtsp://192.168.100.58:8554/stream";
if (!fileName.isEmpty()) {
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
playButton->setEnabled(true);
}
}
void VideoPlayer::play()
{
switch(mediaPlayer.state()) {
case QMediaPlayer::PlayingState:
mediaPlayer.pause();
break;
default:
mediaPlayer.play();
break;
}
}
Videoplayer.h
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
#include <QMediaPlayer>
#include <QMovie>
#include <QWidget>
class QAbstractButton;
class QSlider;
class QGraphicsVideoItem;
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
VideoPlayer(QWidget *parent = 0);
~VideoPlayer();
QSize sizeHint() const { return QSize(800, 600); }
public slots:
void openFile();
void play();
private slots:
void mediaStateChanged(QMediaPlayer::State state);
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void setPosition(int position);
void rotateVideo(int angle);
private:
QMediaPlayer mediaPlayer;
QGraphicsVideoItem *videoItem;
QAbstractButton *playButton;
QSlider *positionSlider;
};
#endif
的.pro
TEMPLATE = app
TARGET = videographicsitem
QT += multimedia multimediawidgets
HEADERS += videoplayer.h
SOURCES += main.cpp \
videoplayer.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/multimediawidgets/videographicsitem
INSTALLS += target
QT+=widgets
还试过这个
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMediaPlayer media;
const QString file = "C:/Users/nick.herniman/Downloads/big_buck_bunny_1080p_h264.mov";
QUrl url(QFileInfo(file).absoluteFilePath());
media.setMedia(url);
media.play();
return a.exec();
}
哪个有效,但只播放文件的音频,任何人都知道我在这里做错了什么(它是否需要在gui窗口中,或者我可以在控制台应用程序中执行此操作)?
答案 0 :(得分:1)
没有什么奇怪的。 : - )
当编译器知道QGraphicsVideoItem
是QGraphicsItem
时,它会有所帮助。并且为了使编译器知道这一点,#include <QGraphicsVideoItem>
应该存在。
我知道,你在Videoplayer.cpp中拥有它。但是main.cpp只看到声明了一个类QGraphicsVideoItem
。你做了很好的前瞻宣言。但这个类是什么......没有办法知道main.cpp中的编译器。将#include <QGraphicsVideoItem>
添加到您的main.cpp。
答案 1 :(得分:1)
在Qt5中尝试这个最简单的例子:
QMediaPlayer *mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
QGraphicsVideoItem *videoItem = new QGraphicsVideoItem;
videoItem->setPos(100,100);
QGraphicsScene *scene = new QGraphicsScene(this);
ui->GV_VideoPlayer->setScene(scene);
scene->addItem(videoItem);
QString fileName = "/root/sampleVideos/videoplayback.mp4";
if(!fileName.isEmpty())
{
mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
}
mediaPlayer->play();
mediaPlayer->setVideoOutput(videoItem);
答案 2 :(得分:0)
QT文档陈述一个非常相似的例子(http://qt-project.org/doc/qt-5.0/qtmultimedia/qgraphicsvideoitem.html)实际上很奇怪,而对象实际上并不相关。除此之外,仅查看QT文档,因为我暂时没有使用QT - 创建虚拟QGraphicsItem
,将其设置为QGraphicsVideoItem
的父项,然后添加该虚拟项到现场帮忙?我猜它会像这样
QGraphicsItem* dummy = new QGraphicsItem;
item = new QGraphicsVideoItem(dummy);
graphicsView->scene()->addItem(dummy);