我已经设置了一个服务器和视频流,这样我就可以使用以下命令行使用ffplay连接到流:
ffplay rtmp://<IP>/path
是否可以使用QMediaPlayer qmedicontent或其他东西连接到此流?
或者我可以用ffserver创建的任何其他类型的流。
使用与ffplay相同的路径会导致"Unsupported url scheme!"
通过进一步的实验,我尝试了ffserver http服务器流,但最终Qt崩溃在MFStreamer::doRead()
显然它应该为MFStreamer调用BeginRead,但它没有。
如何使用QMediaPlayer播放视频流?
编辑:这是我的代码videotest.cpp
#include "videotest.h"
#include <QVBoxLayout>
#include <QVideoWidget>
#include <qmediaplayer.h>
#include <QMediaContent>
#include <QNetworkAccessManager>
#include <QNetworkReply>
struct VideoTest::Private
{
QMediaPlayer * mediaPlayer;
QNetworkAccessManager * networkAccessManager;
QNetworkReply * reply;
};
VideoTest::VideoTest(QWidget *parent)
: QMainWindow(parent)
{
d = new Private;
d->mediaPlayer = new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
d->networkAccessManager = new QNetworkAccessManager(this);
ui.setupUi(this);
QVideoWidget * videoWidget = new QVideoWidget(ui.centralWidget);
videoWidget->show();
QPalette palette = videoWidget->palette();
palette.setColor(QPalette::Background, QColor(0, 0, 0));
videoWidget->setPalette(palette);
ui.videoLayout->addWidget(videoWidget);
d->mediaPlayer->setVideoOutput(videoWidget);
connect(ui.playButton, SIGNAL(clicked()), d->mediaPlayer, SLOT(play()));
connect(ui.pauseButton, SIGNAL(clicked()), d->mediaPlayer, SLOT(pause()));
connect(ui.videoUrlEdit, SIGNAL(editingFinished()), this, SLOT(sourceChanged()));
connect(d->mediaPlayer, SIGNAL(error()), this, SLOT(stateChanged()));
connect(d->mediaPlayer, SIGNAL(stateChanged), this, SLOT(stateChanged()));
}
VideoTest::~VideoTest()
{
delete d;
}
void VideoTest::sourceChanged()
{
d->reply = d->networkAccessManager->get(QNetworkRequest(ui.videoUrlEdit->text()));
if(d->reply)
{
connect(d->reply, SIGNAL(readyRead()), this, SLOT(networkRequestReady()));
}
}
void VideoTest::stateChanged()
{
QString text = ui.textEdit->toPlainText();
text.append("n").append(d->mediaPlayer->errorString()).append(" : ").append(d->mediaPlayer->mediaStatus());
ui.textEdit->setText(text);
}
void VideoTest::networkRequestReady()
{
d->mediaPlayer->setMedia(QMediaContent(), d->reply);
}
videotest.h
#ifndef VIDEOTEST_H
#define VIDEOTEST_H
#include <QtWidgets/QMainWindow>
#include "ui_videotest.h"
class VideoTest : public QMainWindow
{
Q_OBJECT
public:
VideoTest(QWidget *parent = 0);
~VideoTest();
public slots:
void sourceChanged();
void stateChanged();
void networkRequestReady();
private:
Ui::VideoTestClass ui;
struct Private;
Private * d;
};
#endif // VIDEOTEST_H
我找到了一个让它工作的方法。
我放弃了Qt, Qt的人坚持认为它应该工作,但无法产生任何有效的配置。他们说如果你从VLC流式传输,它应该可以工作,但我没有让它工作。我还尝试了ffmpeg, ffserver和nginx rtmp流。我把这些东西用mplayer, ffplay, VLC和一些甚至用Windows媒体播放器,但从来没有QMediaPlayer。
我试图把URL给setMedia。我试图制作一个自定义QIODevice来读取流数据,并将该数据提供给QMediaPlayer,该播放器已初始化为StreamPlayback,但它不会成功读取数据。
最后,我所需要的只是播放流的东西,是一个QWidget,并且没有GPL许可。
我使用libVLC和vlc-qt,它们都工作得很好。
遵循这些指示很容易,但您需要记住将头文件从vlc-qt/windows/vlc_headers/2.2/复制到vlc/sdk/include/vlc/plugins (sic)。这很重要,如果不这样做,编译过程中可能会出现错误。请注意,如果您的平台与我的版本不同,那么这些路径可能会有所不同。另外,当您阅读本文时,可能不需要。
VideoTest.h
#ifndef VIDEOTEST_H_
#define VIDEOTEST_H_
#include <QtWidgets/QMainWindow>
#include "ui_videotest.h"
class VideoTest: public QMainWindow
{
Q_OBJECT
public:
VideoTest(QWidget * p_parent = 0);
~VideoTest();
public slots:
void sourceChanged();
private:
struct Private;
Private * d;
Ui::VideoTestClass ui;
};
#endif
videotest.cpp
#include "videotest.h"
#include <vlc-qt/Common.h>
#include <vlc-qt/Instance.h>
#include <vlc-qt/Media.h>
#include <vlc-qt/MediaPlayer.h>
#include <vlc-qt/WidgetVideo.h>
struct VideoTest::Private
{
VlcInstance * vlcInstance;
VlcMediaPlayer * vlcMediaPlayer;
VlcMedia * vlcMedia;
VlcWidgetVideo * vlcVideoWidget;
};
VideoTest::VideoTest(QWidget * p_parent)
{
d = new Private();
ui.setupUi(this);
d->vlcMedia = 0;
d->vlcInstance = new VlcInstance(VlcCommon::args(), this);
d->vlcMediaPlayer = new VlcMediaPlayer(d->vlcInstance);
d->vlcVideoWidget = new VlcWidgetVideo(this);
d->vlcMediaPlayer->setVideoWidget(d->vlcVideoWidget);
d->vlcVideoWidget->setMediaPlayer(d->vlcMediaPlayer);
ui.videoLayout->addWidget(d->vlcVideoWidget);
connect(ui.playButton, SIGNAL(clicked()), d->vlcMediaPlayer, SLOT(play()));
connect(ui.pauseButton, SIGNAL(clicked()), d->vlcMediaPlayer, SLOT(pause()));
connect(ui.videoUrlEdit, SIGNAL(editingFinished()), this, SLOT(sourceChanged()));
}
VideoTest::~VideoTest()
{
delete d->vlcInstance;
delete d->vlcMediaPlayer;
delete d->vlcMedia;
delete d;
}
VideoTest::sourceChanged()
{
QUrl url(ui.videoUrlEdit->test());
if(url.isValid() == false)
{
return;
}
d->vlcMediaPlayer->stop();
delete d->vlcMedia;
d->vlcMedia = new VlcMedia(url.toString(), d->vlcInstance);
d->vlcMediaPlayer->open(d->vlcMedia);
}
VideoTest.ui
做你自己的,我不为你工作:D
只要确保它有pauseButton, playButton, videoUrlEdit(QLineEdit)和videoLayout,其中视频小部件将被插入
我刚刚设法在QML VideoOutput播放流与c++ QMediaPlayer使用setMedia(QUrl("http://127.0.0.1:8080"));
该流是由VLC媒体播放器使用HTTP到8080端口创建的。我还成功地播放了由VLC媒体播放器创建的流通过通过setMedia(QMediaContent(), &localFile);
传递给QMediaPlayer到本地文件。