1

嘿,stackies 目前正在尝试创建一些仅从本地源播放视频文件的基本代码(尝试使其正常工作,然后尝试实现 rtsp)。下面是我当前的代码。如果有人能告诉我我哪里出错了,那就太好了。

目前代码因错误而失败

C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:58:错误:C2664:'QGraphicsScene::addItem':无法将参数 1 从 'QGraphicsVideoItem *' 转换为 'QGraphicsItem *' 指向的类型是不相关的;转换需要 reinterpret_cast、C 样式转换或函数样式转换

我想我必须将要插入的变量类型转换为 additem。但是不知道如何使用两个 Qclass 来做到这一点。如果有人有一个基本播放器的例子,它只包含一个视频帧并播放第二个你启动程序(最好甚至没有播放按钮)。如果有人有这样的事情,请告诉我,因为这会让我的生活更轻松。主文件

#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();
}

视频播放器.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;
}
}

视频播放器.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 窗口中还是我可以在控制台应用程序中执行它)?

4

3 回答 3

1

没有什么奇怪的。:-)

当编译器知道 aQGraphicsVideoItem是 a时,它会有所帮助QGraphicsItem。为了让编译器知道这一点,#include <QGraphicsVideoItem>应该存在。

我明白了,你在 Videoplayer.cpp 中有它。但是 main.cpp 只看到QGraphicsVideoItem声明了一个类。你做了一个很好的前向声明。但是这个类是什么......对于main.cpp中的编译器没有办法知道。添加#include <QGraphicsVideoItem>到您的 main.cpp。

于 2013-09-03T12:18:55.193 回答
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);
于 2014-08-21T10:49:21.347 回答
0

QT 文档说明了一个非常相似的示例( http://qt-project.org/doc/qt-5.0/qtmultimedia/qgraphicsvideoitem.html),而这些对象实际上并不相关,这真的很奇怪。除此之外,由于我有一段时间没有使用 QT,因此只需查看 QT 文档 - 创建一个 dummy QGraphicsItem,将其设置为您的父QGraphicsVideoItem项,然后将该 dummy 项目添加到场景中是否有帮助?我想它会像这样

QGraphicsItem* dummy = new QGraphicsItem;
item = new QGraphicsVideoItem(dummy);
graphicsView->scene()->addItem(dummy);
于 2013-09-03T12:00:55.930 回答