qmediaplayer获取流类型_如何使用QMediaPlayer播放流

I have set up a server and video streaming so that I can connect to the stream with ffplay using the following command line:

ffplay rtmp:///path

Is it possible to use QMediaPlayer QMediaContent or something to connect to this stream?

Or maybe any other kind of stream I can create with ffserver.

using the same path as with ffplay results in "Unsupported url scheme!"

With further experiments i have tried ffserver http server streaming, but that ended with Qt crashing in MFStreamer::doRead()

Apparently it should have called BeginRead for MFStreamer but it didn't.

How do i play video streams with QMediaPlayer?

Edit: here's my code

videotest.cpp

#include "videotest.h"

#include

#include

#include

#include

#include

#include

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

#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

解决方案

I found a way to make it work.

I gave up on Qt. The guys at Qt were insistent that it should work, but were unable to produce any configuration that works. They said that it should work if you stream from VLC, but I didn't get it to work. I also tried ffmpeg, ffserver and nginx rtmp streaming. I got these things working with mplayer, ffplay, VLC and some even with Windows Media Player, but never QMediaPlayer.

I tried to just give the URL to setMedia.

I tried to make a custom QIODevice to read the stream data and give that data to QMediaPlayer which was initialized with StreamPlayback, but it just would not succeed in reading the data.

In the end, all I needed was something to play a stream, is a QWidget and isn't GPL licensed.

I used libVLC and vlc-qt both of which work wonderfully.

Following these instructions was easy, but you need to remember to copy the header files from vlc-qt/windows/vlc_headers/2.2/ to vlc/sdk/include/vlc/plugins (sic). This is important, if you don't do this you might get errors during compilation. Note that these paths might be different if you have different versions of your platform doesn't match mine. Also, it might not be necessary when you read this.

VideoTest.h

#ifndef VIDEOTEST_H_

#define VIDEOTEST_H_

#include

#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

#include

#include

#include

#include

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

Make your own, I don't work for you :D

Just make sure that it has pauseButton, playButton, videoUrlEdit(QLineEdit) and videoLayout where the video widget will be inserted.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值