Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

play rtsp stream QMultimedia



  • hi,

    i tried to play a rtsp stream, that is streammed with VLC on localhost

    #include "VideoPlayer2.h"
    
    #include <QtWidgets>
    #include <qvideowidget.h>
    #include <qvideosurfaceformat.h>
    #include <QGraphicsScene>
    #include <QGraphicsVideoItem>
    
    VideoPlayer2::VideoPlayer2(QWidget *parent)
        : QWidget(parent)
        , mediaPlayer(0, QMediaPlayer::StreamPlayback)
        , playButton(0)
        , positionSlider(0)
        , errorLabel(0)
    {
        QGraphicsView * view=new QGraphicsView(this);
        //QVideoWidget *videoWidget = new QVideoWidget;
        QGraphicsVideoItem *videoItem= new QGraphicsVideoItem();
        QGraphicsScene * scene =new QGraphicsScene(this);
        scene->addItem(videoItem);
        view->setScene(scene);
    
        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)));
    
        errorLabel = new QLabel;
        errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
    
        QBoxLayout *controlLayout = new QHBoxLayout;
        controlLayout->setMargin(0);
        controlLayout->addWidget(openButton);
        controlLayout->addWidget(playButton);
        controlLayout->addWidget(positionSlider);
    
        QBoxLayout *layout = new QVBoxLayout;
       // layout->addWidget(videoWidget);
    	layout->addWidget(view);
        layout->addLayout(controlLayout);
        layout->addWidget(errorLabel);
    
        setLayout(layout);
    
        mediaPlayer.setVideoOutput(videoItem);
    	//mediaPlayer.setMedia(
        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)));
        connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
        view->show();
    }
    
    VideoPlayer2::~VideoPlayer2()
    {
    }
    
    void VideoPlayer2::openFile()
    {
        errorLabel->setText("");
    
       // QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
    	QString fileName ="rtsp://localhost:8554/toto";
        if (!fileName.isEmpty()) {
            mediaPlayer.setMedia(QUrl(fileName));
            playButton->setEnabled(true);
        }
    }
    
    void VideoPlayer2::play()
    {
        switch(mediaPlayer.state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer.pause();
            break;
        default:
            mediaPlayer.play();
            break;
        }
    }
    
    void VideoPlayer2::mediaStateChanged(QMediaPlayer::State state)
    {
        switch(state) {
        case QMediaPlayer::PlayingState:
            playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        default:
            playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    }
    
    void VideoPlayer2::positionChanged(qint64 position)
    {
        positionSlider->setValue(position);
    }
    
    void VideoPlayer2::durationChanged(qint64 duration)
    {
        positionSlider->setRange(0, duration);
    }
    
    void VideoPlayer2::setPosition(int position)
    {
        mediaPlayer.setPosition(position);
    }
    
    void VideoPlayer2::handleError()
    {
        playButton->setEnabled(false);
        errorLabel->setText("Error: " + mediaPlayer.errorString());
    }
    
    

    But it does not work
    here is what my error label says: " error: Failed to load source "
    Can you please tell me why ?
    help help I'm desperate

    by the way, I'm using Qt5.3.2 visualStudio 2010
    Thank you