如何使用QMediaPlayer播放流

时间:2015-05-28 12:59:48

标签: c++ qt video rtmp ffserver

我已经设置了服务器和视频流,以便我可以使用以下命令行使用ffplay连接到流:

ffplay rtmp://<IP>/path

是否可以使用QMediaPlayer QMediaContent或其他东西连接到此流?

或者我可以使用ffserver创建任何其他类型的流。

使用与ffplay相同的路径导致&#34;不支持的网址方案!&#34;

通过进一步的实验,我尝试了ffserver http服务器流,但结果是在MFStreamer :: doRead()中的Qt崩溃

显然它应该为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

2 个答案:

答案 0 :(得分:4)

我找到了一种方法让它发挥作用。

我放弃了Qt。 Qt的人坚持认为它应该可行,但无法生成任何有效的配置。他们说如果从VLC流式传输它应该可以工作,但我没有让它工作。我也尝试了ffmpeg,ffserver和nginx rtmp流媒体。我将这些东西与mplayer,ffplay,VLC和一些甚至与Windows Media Player配合使用,但从未使用过QMediaPlayer。

我尝试将网址提供给setMedia。 我尝试制作一个自定义QIODevice来读取流数据并将该数据提供给使用StreamPlayback初始化的QMediaPlayer,但它只是不会成功读取数据。

最后,我需要的只是播放流,是QWidget而不是GPL许可。

我使用了libVLCvlc-qt两者都非常有效。

关注these instructions非常简单,但您需要记住将头文件从 vlc-qt / windows / vlc_headers / 2.2 / 复制到 vlc / sdk / include / vlc / plugins (原文如此)。这很重要,如果不这样做,编译过程中可能会出错。请注意,如果您的平台的不同版本与我的版本不匹配,则这些路径可能会有所不同。此外,当您阅读本文时,可能没有必要。

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。

答案 1 :(得分:0)

我刚刚使用setMedia(QUrl("http://127.0.0.1:8080"));使用C ++ QMediaPlayer在QML VideoOutput中播放流。
该流是由VLC媒体播放器使用HTTP到8080端口创建的。通过通过setMedia(QMediaContent(), &localFile);将VLC媒体播放器创建的流传递到QMediaPlayer,我还成功地将其播放到本地文件。